Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Traversing two different linked list

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Traversing two different linked list

    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);
    	}
    		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();
    				}
    Last edited by Usoda; March 10th, 2012 at 05:50 PM.


Similar Threads

  1. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  2. Linked List Help
    By BuhRock in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 08:43 AM
  3. linked list help
    By tjoney in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2011, 06:54 PM
  4. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM
  5. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM

Tags for this Thread