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

Thread: what's wrong with my linked list

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default what's wrong with my linked list

    i receive the following errors when excuting this code

    Exception in thread "main" java.lang.NullPointerException
    at Node.createNode(Node.java:28)
    at LinkedList.main(LinkedList.java:6)

    Process completed.


     
    public class Node
    {
        String data;
    	Node prev;
    	Node next;
    	int index;	
     
    		int cnt=0;
    		private Node Header;
     
     public Node()
     {
     	this.data=null;
     	this.prev=null;
     	this.next=null;
     	this.index=getIndex();
     }
     public Node createNode(String x)
     {
     	Node n=new Node();
     	     if(n.index==1)
     	     {
     	     	Header.data=x;
     	     	return Header; //this to distinguish the first node created to be the header and works as reference 
     	     }
     	     else
     	     {
     	     	Header.next=n.prev;
     	     	Header.prev=n.next;
     	     	n.data=x;
     	     	return n; // return new node
     	     }
     }
     
     public int getIndex()
     {
     	return cnt++;
     }
     
    }

    Main:

    public class LinkedList
    {
    	public static void main (String[] args)
    	{
    		Node n=new Node();
    		n.createNode("a");
    		System.out.println (n.data);
    		System.out.println (n.cnt);
    		System.out.println (n.getIndex());
    	}
     
    }
    Last edited by amr; February 12th, 2011 at 03:51 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: what's wrong with my linked list

    Where you initialize the variable Header? It need to be initialized somewhere.

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    hi,
    Header should be initialized to what?? and why?

  4. #4
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    Quote Originally Posted by tiagoufrj View Post
    Where you initialize the variable Header? It need to be initialized somewhere.
    hi
    Header should be initialized to what? and why?

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: what's wrong with my linked list

    Quote Originally Posted by amr View Post
    hi
    Header should be initialized to what? and why?
    Should be initialized to the head of your list, otherwise the code will throw NullPointerException.

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: what's wrong with my linked list

    Because the variable Header is null when the line Header.next=n.prev; is running.

    >Header should be initialized to what?

    Thats depends on what you want. What does variable Header means?

  7. #7
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    Quote Originally Posted by tiagoufrj View Post
    Because the variable Header is null when the line Header.next=n.prev; is running.

    >Header should be initialized to what?

    Thats depends on what you want. What does variable Header means?
    the node Header is a reference node, to which , i can link the most recent or the last added node to.
    in other words, if i have 5 nodes, in double linkedList, the last node should be connected to the first node .
    such a way that, the next of the last node points to the previous of the first node(Header) and the previous of the last node(5th node) points to the next of the preceeding node(4th).

    isnt the double linked list works so???

  8. #8
    Junior Member benglish's Avatar
    Join Date
    Feb 2011
    Location
    LUV country
    Posts
    21
    My Mood
    Busy
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    Moreover, in the constructor, you've not initiated (this.index)

    it may be the other reason your program doesn't work well

  9. #9
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    Quote Originally Posted by benglish View Post
    Moreover, in the constructor, you've not initiated (this.index)

    it may be the other reason your program doesn't work well
    hi,
    this.index is initialized to getIndex();

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: what's wrong with my linked list

    tiagoufrj is correction. The Header needs to be initialized. Step through the createNode function one by one, when you reach Header.data=x; you should see Header is null. To fix, you should first assign header to the 'head'/first node in the List
    if(n.index==1)
             {
                Header = n;//assign header to an instance before trying to access its values
                Header.data=x;
                return Header; //this to distinguish the first node created to be the header and works as reference
             }

    You might also want to evaluate the contents of the else clause as well. I'm not entirely clear as to how you wish to implement the List, but if you want to add the new Node to the end of the list and Header is the first in the list, then you should migrate through until you reach a next node that is null, then assign it to the new node.

  11. #11
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my linked list

    Quote Originally Posted by copeg View Post
    tiagoufrj is correction. The Header needs to be initialized. Step through the createNode function one by one, when you reach Header.data=x; you should see Header is null. To fix, you should first assign header to the 'head'/first node in the List
    if(n.index==1)
             {
                Header = n;//assign header to an instance before trying to access its values
                Header.data=x;
                return Header; //this to distinguish the first node created to be the header and works as reference
             }

    You might also want to evaluate the contents of the else clause as well. I'm not entirely clear as to how you wish to implement the List, but if you want to add the new Node to the end of the list and Header is the first in the list, then you should migrate through until you reach a next node that is null, then assign it to the new node.
    thank you.........

    Kind Regards;
    Schuss

Similar Threads

  1. Finding min in linked list
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 02:06 AM
  2. [SOLVED] Linked List Help
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 4th, 2011, 10:32 PM
  3. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  4. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 07:23 PM