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: null pointer in a linked list

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

    Default null pointer in a linked list

    I've been having some "fun" with my program Im making for a class im taking. I thought I handled all of the null pointers correctly but I guess not, heres what I currently have.
    public class LinkedList
    {
       private LinkedListNode first;       //holds the first/head of the list
       private LinkedListNode last;        //holds the last/tail of the list
       private LinkedListNode current;     //holds the current node you are at
     
     
    //List constructor
       public LinkedList()
       {
          first    = null;                 //creates the list with no head
          last     = null;                 //creates the list with no tail
          current  = first;                //starts you at the head
       }
    //Linked list node class
       public class LinkedListNode
       {
          private Object info;              //the information in the node
          private LinkedListNode next;  //a node that points to the next in the list
     
          public LinkedListNode()
          {
             next = null;                     //always creates the first node pointing to nothing
          }
       }
       public void insertFirst(Object new_info)
       {
          LinkedListNode temp = new LinkedListNode();
          temp.info = new_info;
          if(first == null)
          {
             temp = first;
             temp = last;
          }
          else
          {
             temp.next = first;
             temp = first;
          }
       }
    I have some experience in C++ dealing with pointers, am I coding in C++ with out realizing it?

    --- Update ---

    Wait found part of the problem, found the issue in the insertFirst method... the head and tail werent being set correctly, i was effectively deleting the node i was inserting. I just got the current node to work properly now


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: null pointer in a linked list

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: null pointer in a linked list

    Important to know is that java does not have pointers. It has references instead. There has been plenty of discussion on the subject so you can do some research to make sure you have it clear in your head.
    Improving the world one idiot at a time!

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Replies: 1
    Last Post: October 25th, 2012, 03:03 PM
  4. linked list null pointer exception
    By sonicjr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2012, 07:10 AM
  5. 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, 07:55 PM