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 3 of 3

Thread: linked list null pointer exception

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default linked list null pointer exception

    I am trying to create a Linked List class for my programming homework. I need to make my insert method perform the lookup method first so that I cannot insert any duplicate elements. the code works and compiles fine without my if(lookup==false) conditional, but with my current code below I get a null pointer exception at temp.data.equals(x) in the lookup method. Here is my linkedlist class:
    public class linkedList implements myLinkedList
    {
     
    	public myNode head = null;
    	public myNode tail = null;
    	public myNode temp = null;
     
    	public linkedList()
    	{
    		myNode node = new myNode();
    		node = null;
    	}
    	//inserts objects to the end of the list if not already there
    	public void insert(Object x)
    	{
    		if(lookup(x) == false)
    		{
    			if(head==null)
    			{
    				head = new myNode();
    				tail = new myNode();
    				head.data = x;
    				head.next = tail;
    				tail = head;
    			}
    			else
    			{
    				tail.next = new myNode();
    				tail = tail.next;
    				tail.data = x;
    			}
    		}
    	}
    	public void delete(Object x)
    	{
    	}
    	public boolean lookup(Object x) //interface in lab says public Object, but directions specify for it to return boolean value
    	{
    		temp = head;
    		while(temp != null)
    		{
    			if(temp.data.equals(x)) return true;
    			temp = temp.next;
    		}
    		return false;
    	}
    	public boolean isEmpty()
    	{
    		if(head==null) return true;
    		else return false;
    	}
    	public void printList()
    	{
    		myNode index = head;
    		while(index != null)
    		{
    			System.out.println(index.data.toString());
    			index = index.next;
    		}
    	}
    }
    this will work the first time i try to insert an object, but any subsequent attemps result in the null pointer exception. I am not sure what the problem is, any advice would be extremely helpful
    Last edited by sonicjr; February 16th, 2012 at 10:04 PM.


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: linked list null pointer exception

    where does the nullpointer points out in your line of code? , in that way you/we can determine or atleast have a clue to what causes the exception.. i think its on the constructor of your class

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: linked list null pointer exception

    I get a null pointer exception at temp.data.equals(x) i
    What variable has a null value? Find which one and then backtrack in the code to see why that variable does not have a valid value.

Similar Threads

  1. [B]Null Pointer Exception[/B]-- Please Help!!!
    By entwicklerin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 24th, 2012, 11:24 AM
  2. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  3. Singly Linked List of Integers, get(int i) function throws Null Pointer Exception
    By felixtum2010 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 06:55 PM
  4. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM