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; } Thank you all very much!! -BoltedDown |
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. |
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. |
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; }
__________________
"Don't believe everything you read on the internet. Except this. Well, including this, I suppose." -- Douglas Adams |
Tags |
java |
|
|