Results 1 to 4 of 4
  1. #1
    cannonal is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Unhappy Cannot find symbol error

    Oh it's there. The symbol is there. I've bee staring at it for an hour. Ugh. It's making me grumpy.
    Here's my code. Originally, it had six or seven additional methods, but since they were all giving me the same ugly error, I set them aside for a while.

    Java Code:
    import java.io.*;
    
    //class to instantiate Item objects
    public class Item
    {
    	public Item()
    	{
    		long num;
    		String descrip;
    		double bCost;
    		double sPrice;
    		int amount;
    	}
    
    	public Item(long num, String d, double cost, double price, int cAmount)
    	{
    		long itemNum = num;
    		String descrip = d;
    		double wCost = cost;
    		double sPrice = price;
    		int amount = cAmount;
    	}
    }
    
    //class to hold inventory list
    class Inventory
    {
    	
    	//instantiate inventory list
    	public Inventory()
    	{
    		Item[] itemList;
    		long[] freeNums;
    		int cnt = 0;
    	}	
    
    	public Inventory(int size)
    	{
    		Item[] itemList = new Item[size];	//actual Inventory 
    		long[] freeNums = new long[size];	//list of possible numbers for new items
    		int cnt = 0;						//keep track of new items added
    
    		//fill freeNums
    		for(int i = 0; i < size; i++)
    			freeNums[i] = size + 1;		//ie; 101 - 201
    	}
    
    }
    
    //class to manipulate current inventory
    class InvMaintenance
    {		
    	final static int MAX = 100;			//constant for array length	
    	Inventory c = new Inventory(MAX);	//instantiate Inventory object
    	
    	public static void main(String[] args)
    	{
    		InvMaintenance inv = new InvMaintenance();
    			
    		try
    		{
    			//Check for previous save file
    				//if File Not Found, create new file		
    
    			//create an OutputStream to write data to a file
    			File saveFile = new File("inven.txt");
    			FileOutputStream fos = new FileOutputStream(saveFile);
    			BufferedOutputStream bos = new BufferedOutputStream(fos);
    			ObjectOutputStream oos = new ObjectOutputStream(bos);
    			
    			oos.writeObject(inv.c);			//write initial Inventory to file
    			
    			//Output options
    				/*  Inventory Maintenance
    					1) Add Item
    					2) Remove Item
    					3) Sell Item
    					4) Receive Item
    					5) Display Inventory
    					6) Quit
    					Please Select NUMBER: */
    	
    			//switch on options
    				//call appropriate method
    			
    	
    			oos.writeObject(inv.c);
    			oos.close();
    		}
    		
    		catch(Exception e)	{}
    	}
    	
    	//method to traverse entire inventory
    	public void display()
    	{
    				
    		for(int i = 0; i < c.cnt; i++)
    		{
    			System.out.println("Item number:" + c.itemList[i].itemNum);
    			System.out.println("Description:" + c.itemList[i].descrip);
    			System.out.println("Wholesale cost:" + c.itemList[i].wCost);	
    			System.out.println("Sale price:" + c.itemList[i].sPrice);
    			System.out.println("Balance on hand:" + c.itemList[i].amount);
    		}
    	}
    
    
    	
    }

    And here is my javac error:

    Java Code:
    Item.java:104: cannot find symbol
    symbol  : variable cnt
    location: class Inventory
    		for(int i = 0; i < c.cnt; i++)
    		                    ^
    Item.java:106: cannot find symbol
    symbol  : variable itemList
    location: class Inventory
    			System.out.println("Item number:" + c.itemList[i].itemNum);
    			                                     ^
    Item.java:107: cannot find symbol
    symbol  : variable itemList
    location: class Inventory
    			System.out.println("Description:" + c.itemList[i].descrip);
    			                                     ^
    Item.java:108: cannot find symbol
    symbol  : variable itemList
    location: class Inventory
    			System.out.println("Wholesale cost:" + c.itemList[i].wCost);	
    			                                        ^
    Item.java:109: cannot find symbol
    symbol  : variable itemList
    location: class Inventory
    			System.out.println("Sale price:" + c.itemList[i].sPrice);
    			                                    ^
    Item.java:110: cannot find symbol
    symbol  : variable itemList
    location: class Inventory
    			System.out.println("Balance on hand:" + c.itemList[i].amount);
    			                                         ^
    6 errors
    I very clearly see that there is a cnt and itemList variable in my Inventory constructor. My Terminal disagrees. But it's been a long night.

    Any help will be incredibly greatly appreciated!

  2. #2
    Join Date
    Jan 2010
    Posts
    19
    Rep Power
    0

    Default Re: Cannot find symbol error

    You should declare cnt and itemList as members variables of the class.
    Take a look in these sites, maybe it will help you:
    Declaring Member Variables (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    and
    http://www.java-made-easy.com/variable-scope.html
    Last edited by Gilvan Justino; 01-22-2012 at 04:20 PM.

  3. #3
    cannonal is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Cannot find symbol error

    Huh, look at that. It worked! I could have sworn you could have a constructor with blank fields and still access them... Does that technique sound familiar at all?

    Anyway, thank you! You just took away 15 or so errors in my entire program. Thankyouthankyouthankyou.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Cannot find symbol error

    Quote Originally Posted by cannonal View Post
    Huh, look at that. It worked! I could have sworn you could have a constructor with blank fields and still access them... Does that technique sound familiar at all?
    No. .

Similar Threads

  1. 'error: cannot find symbol' please help
    By Bahirji Naik in forum New To Java
    Replies: 4
    Last Post: 12-21-2011, 09:03 AM
  2. Cannot Find Symbol error
    By mrgreenacid in forum New To Java
    Replies: 13
    Last Post: 05-16-2011, 08:28 AM
  3. Cannot find symbol error
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 11:13 AM
  4. Replies: 9
    Last Post: 10-18-2008, 09:26 PM
  5. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •