Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 01-25-2005, 05:16 PM   #1 (permalink)
Crazy
 
[JAVA] java.lang.NullPointerException

Ok guys, i've got some new code ready for ya...

Code:
java.lang.NullPointerException
	at restaurant1model.ProductCatalog.getSpecification(ProductCatalog.java:36)
	at restaurant1model.Register.enterItem(Register.java:54)
	at restaurant1view.Program1.main(Program1.java:54)
Code:
	public ProductSpecification getSpecification(String itemCode)
	{
		ProductSpecification item = null;
		boolean foundFlag = false;
		//System.out.println(itemCode);
		for (int i = 0; i < productSpecifications.length && !foundFlag; i++)				//for (int i = 0;!(i >= productSpecifications.length || foundFlag);	i++)		
		{
			if (itemCode.equals(productSpecifications[i].getItemId()))
			{
				item = productSpecifications[i];
				foundFlag = true;
			}
		}
		return item;
	}
Code:
	public boolean enterItem(String upc, int quantity)
	{
		boolean flag = false;
		if (sale == null || sale.isComplete())
		{
			sale = new Sale();
		}
		ProductSpecification productionSpecification;
		productionSpecification =
			(ProductSpecification) productCatalog.getSpecification(upc);
		if (productionSpecification != null)
		{
			flag = sale.makeLineItem(productionSpecification, quantity);
		}
		else
		{
			flag = false;
		}
		return flag;
	}
Code:
case 1 : // Event 1 - Enter Item
	{
		boolean doSaleAgain;
		do
		{
		int tableNumber = Program1.readInt("Enter Table Number");
		String itemCode = JOptionPane.showInputDialog("Enter Item Code", "X00");
		int quantity = Program1.readInt("Enter Quantity");
		register.enterItem(itemCode, quantity);
		doSaleAgain =
		readBoolean("Do you wish to enter another item?");
	}
	while (doSaleAgain);
	doAgain = true;
	break;
}
The bottom three snippets are all assosciated with their respective parts of the original error message (top). Let me know what you want to know and I will do my best to answer.

Thank you all very much!!

-BoltedDown
BoltedDown is offline  
Old 01-25-2005, 06:37 PM   #2 (permalink)
Insane
 
Location: Ithaca, New York
productSpecifications

in your first set of code. Is this a class variable? because it doesn't seem to exist anywhere else in your code.
__________________
And if you say to me tomorrow, oh what fun it all would be.
Then what's to stop us, pretty baby. But What Is And What Should Never Be.
fckm is offline  
Old 01-26-2005, 12:56 AM   #3 (permalink)
Insane
 
I would guess you're calling getItemID() on an uninitialized (NULL) member of the productSpecifications array. I don't know the extent of your program, but a HashMap object might be better suited for your list of ProductSpecifications rather an an array, ArrayList, or whatever you're using to index it.

Also, as a tip, you can use the "break;" command in that for loop so you don't have to use a boolean flag. It simply breaks out of the inner most loop.
Anomaly_ is offline  
Old 01-27-2005, 07:53 PM   #4 (permalink)
Crazy
 
Location: San Diego, CA
Out of curiosity, why not do this?

Code:
        public ProductSpecification getSpecification(String itemCode)
	{
		//System.out.println(itemCode);
		for (int i = 0; i < productSpecifications.length; i++)			
		{
			if (itemCode.equals(productSpecifications[i].getItemId()))
			{
				return productSpecifications[i];
			}
		}
		return null;
	}
As for your error, it sounds like ProductSpecifications is undefined when you run this function.
__________________
"Don't believe everything you read on the internet. Except this. Well, including this, I suppose." -- Douglas Adams
Rangsk is offline  
 

Tags
java


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 11:52 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76