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

Thread: Singly Circular Linked List Error

  1. #1
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Singly Circular Linked List Error

    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

    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:

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

    thanks in advance


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Singly Circular Linked List Error

    When you create the head with insert(), to what node should its next reference refer?

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    clydefrog (March 6th, 2012)

  4. #3
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Singly Circular Linked List Error

    Thank you for the prompt reply!

    hmmm...

    so you mean somethng like this?

    if(head == null)
    {
    	head = temp;
            head.next = head;
    	System.out.println("hit");
     
    }

    if i put that. the error goes away, but it doesnt add anything aside from the first entry i think.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Singly Circular Linked List Error

    Step through your code and write out on paper what is happening. On first insert you create a node:

    Node {next = null}

    When you loop through upon second insertion, you end up setting current to the next value, which is null

  6. The Following User Says Thank You to copeg For This Useful Post:

    clydefrog (March 6th, 2012)

  7. #5
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Singly Circular Linked List Error

    well i tried tracing it out on paper, but i'm not sure i did it correctly. From what i see on my paper, is the issue here the fact that the first node is constantly pointing to itself?

    Not sure how to explain it but:

    - So we make our first node, and its pointer is null.
    - i make that node my head, give it a value of 1, and set its pointer to point to itself
    - when i try to add another node, it never hits the while loop because the current is already pointing the head.

    Is this correct? How can i remedy this?

  8. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Singly Circular Linked List Error

    Quote Originally Posted by clydefrog View Post
    How can i remedy this?
    Your code might be working. Your toString() method won't: you set current = head and then while(current != head). Consider changing that so that the loop executes at least once for the head node if it exists

  9. The Following User Says Thank You to Sean4u For This Useful Post:

    clydefrog (March 6th, 2012)

  10. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Singly Circular Linked List Error

    is the issue here the fact that the first node is constantly pointing to itself?
    I see nowhere in your original code in which you define the first node pointing to itself. See post #3

  11. #8
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Singly Circular Linked List Error

    Quote Originally Posted by Sean4u View Post
    Your code might be working. Your toString() method won't: you set current = head and then while(current != head). Consider changing that so that the loop executes at least once for the head node if it exists
    Sorry i didnt reply sooner, i had to leave the house for a few hours.

    wtf? Yeah it was the toString() method, it appears to be working correctly now. lol thank you so much.

    Now just another quick question: From what i can tell, it appears as though my circular linked list is implemented correctly. How can test to see if its actually circular?
    Last edited by clydefrog; March 5th, 2012 at 08:54 PM.

Similar Threads

  1. 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
  2. Need help to understanding with Doubly-linked circular Lists
    By mrdeath9x in forum Java Theory & Questions
    Replies: 3
    Last Post: May 5th, 2011, 03:51 AM
  3. [SOLVED] Circular Linked List---Need Help ASAP (before midnight)
    By The Dark Mathematician in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 17th, 2011, 02:24 PM
  4. Help with a doubly linked circular list
    By TeamRival in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 10:59 PM
  5. circular linked list
    By student123xyz in forum Collections and Generics
    Replies: 4
    Last Post: August 19th, 2009, 10:40 AM