-
May 18th, 2011, 10:16 PM
#1
help!
Im trying to make a program that simulates a vending machine while using stacks and queues, but while sending out the method to add the objects into my linked list I keep getting a null pointer exception. heres the code with MachineRow being an interface and FIFO being a linkedList
public static void loadFIFO(int size, Scanner keyboard, MachineRow FIFO)
throws FileNotFoundException {
System.out.print("Enter filename: ");
String fileopen = keyboard.nextLine();
Scanner inputFile = new Scanner(new File(fileopen));
int j = 0;
while (inputFile.hasNext() && j < size) {
String name = inputFile.nextLine();
String type = inputFile.nextLine();
String upc = inputFile.nextLine();
VendingProduct add = new Product(name, type, upc);
if(FIFO.add(add)==true){
j++;
}
}
}
and heres the method in the FIFORow class
public boolean add(VendingProduct product) {
FIFOrow.addFirst(product);
boolean added=true;
return added;
}
please help if you can!
-
May 19th, 2011, 06:44 AM
#2
Re: help!
1. Use code tags, preserves readable formatting of the code.
2. Post relevant information. - Stack trace (if any), code samples that shed actual light on your issue.
3. Please mark to which line of code the exception points to. (eg "<- Exception trown here").
4. Describe any non-local variables used in your code sample, what is their value when used in a typical test run, and more so, what is their purpose.
In the code you gave us, there can be roughly a millon things wrong. And without more specific information... why try to extend ones telepathic abilities to quess the surrounding frameworks?
That said... your handling of Scanner reading is almost certain to fail. Think on the differences in calling next() and nextLine() methods. Think how you can make your file format and its reading more secure. Based on the info you gave me, that would (at least) be a part to re-design.
Also the boolean returned by our other sample method has absolutely no purpose. Do not do that.
-
May 19th, 2011, 08:46 AM
#3
Re: help!
My crystal ball has gone on the blink so I'm going to have to guess and my guess is you haven't initialized the FIFOrow variable.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|