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.
Code java:
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:
Code java:
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());
}
}
Re: what's wrong with my linked list
Where you initialize the variable Header? It need to be initialized somewhere.
Re: what's wrong with my linked list
hi,
Header should be initialized to what?? and why?
Re: what's wrong with my linked list
Quote:
Originally Posted by
tiagoufrj
Where you initialize the variable Header? It need to be initialized somewhere.
hi
Header should be initialized to what? and why?
Re: what's wrong with my linked list
Quote:
Originally Posted by
amr
hi
Header should be initialized to what? and why?
Should be initialized to the head of your list, otherwise the code will throw NullPointerException.
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?
Re: what's wrong with my linked list
Quote:
Originally Posted by
tiagoufrj
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???
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
Re: what's wrong with my linked list
Quote:
Originally Posted by
benglish
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();
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
Code :
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.
Re: what's wrong with my linked list
Quote:
Originally Posted by
copeg
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
Code :
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