Hi,

I'm trying to make a singly circular linked list, however i happen to be getting nullpoiterexception in my code. I've never worked with circular linked list, so im not to certain if my approach is even correct. I have a feeling its not a big error and it can be remedied quickly, but i just cant see the solution.

I think i know what the problem is, but i cant figure out a way to solve it:

-basically at line 26 (i've marked it with comments), i have a while loop that should iterate through the linked list and stop just before the 'head' (remember this is a circular list, so the last node points back to the first which is the 'head')

-originally the head is 'null' so it adds the first node, and sets the current node to point to head

-when it attempts to add the second node, im guessing while loop flips out, because it says "current.getNext() != head" but current is equal to head...

so yeah, thats about as far as my debugging has gone. But i know it has something to do with the fact that its circular. because it works as a linear linked list. if you want to try it out as a linear linked list, i've included a few comments to tell you which lines to modify; there are 3 in total.

I would be very grateful if someone could take a look and help me out :)

Code :

package project3; //circular linked list class Hex { Node head; Hex() { head = null; } public void insert(int n) { Node temp = new Node(n); Node current = head; //if the list is empty, then the new node equal to head if(head == null) { head = temp; System.out.println("hit"); } else { //Starting from head, move through the list until you get to head again while(current.getNext() != head) //<------- ***Error*** (if want linear, change this line to, while(current.getNext() != null) { current = current.getNext(); } //set the last node's pointer to the new node current.setNext(temp); //set the pointer of the last node back to the first node temp.next = head; //if you want linear, delete this line } } //node class class Node { int element; Node next; Node() { } Node(int element) { this.element = element; next = null; } Node(int element, Node next) { this.element = element; this.next = next; } public int getElement() { return element; } public void setElement(int element) { this.element = element; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } public String toString() { Node current = head; String output = ""; while(current != head) //if want this linear, change this line to while(current.getNext() != null) { output += "[" + current.getElement() + "]"; current = current.getNext(); } return output; } } public class Project3 { public static void main(String[] args) { Hex h1 = new Hex(); h1.insert(1); h1.insert(2); h1.insert(3); h1.insert(4); h1.insert(5); h1.insert(6); System.out.println(h1); } }

Error:

Code :

Exception in thread "main" java.lang.NullPointerException at project3.Hex.insert(Project3.java:26) at project3.Project3.main(Project3.java:102)

thanks in advance