I have a Product class that is the parent of 3 other classes; Books, Movies, Music. And the Product class connects to the Store class that holds the Store's location.
If I need to find a certain Product I have to traverse the list of each the Store class and the Product class. However, my code is throwing a NullPointerExeption.
When I debug it shows that pHead = null.
I am thinking the error is within my addProductBook method and is not appointing the first product to head? Or my findProduct method holds the error.
My constructor and addProductBook is in my Store Class
public Store() {
head = new Product(null, null, 0);
}
public void addProductBook() {
if(head == null) {
head = new Book(product, name, price, author, ISBN);
return;
}
else {
Book newBook = new Book(product, name, price, author, ISBN);
while(newBook.getNext() != null) {
newBook = (Book) newBook.getNext();
newBook.setNext(newBook);
Store.mainMenu();
}
}
}
getProduct()
returns product
getName() - String
returns name(Items name)
findProduct is in a different Class
public static void findProduct(String item) {
Store current = head;
Product pHead = current.getProduct();
pHead.getName(); // This is a test spot that throws a NullPointerException
while(pHead != null && current != null){
if((pHead.getName().equals(item)))
pHead.printProduct();
else
current.getNextStore();
pHead = pHead.getNext();
}