Anarantt
Anarantt

Reputation: 289

Get Exception in thread "main" java.lang.NullPointerException with LinkedList

Simple Linked List

public class List_manager {
        Entry first;
        Entry last;
        public void add(String el) {
            if (isEmpty()) { first=new Entry(el); last=first; return; }
            new Entry(el,last);
        }

        public String get() {
            Entry temp=first;
            first=first.next;
            return temp.data;
        }

        public boolean isEmpty() {
            return first==null;
        }
        private class Entry {
            String data;
            Entry next;
            public Entry(String data,Entry to) {
                this.data=data;
                to.next=this;
                to=this;
            }
            public Entry(String data) {
                this.data=data;
            }
        }
    }

#The main class#

I added 3 element and list contains only 2... why?

  public class Main {
        public static void main(String[] args) {
            List_manager l=new List_manager();
            l.add("1");
            l.add("2");
            l.add("3");
            System.out.println(l.get());
            System.out.println(l.get()); // Why here output: "3"??
            System.out.println(l.get()); // here is an error occurs
        }
    }

I really don`t get why list contains 2 elements?
Why it ignores 2nd added element?

Upvotes: 0

Views: 796

Answers (2)

ulltramule
ulltramule

Reputation: 11

to=this; This sentence have no influence on variable 'last', because veriable 'to' is formal parameter, while variable 'last' is actual parameter. So, when you executed this sentence "to = this;" the value of
variable 'last' was not changed to next.That's mean variable 'last' always pointed to the first element.

my change is : new Entry(el,last); --> last = new Entry(el,last); Things look better.

Upvotes: 1

Makoto
Makoto

Reputation: 106430

Think about what your get method is doing. You already noticed some aberrant behavior with it.

public String get() {
    Entry temp=first;
    first=first.next;
    return temp.data;
}

What happens the first time I call this?

  • temp gets whatever first is pointing to
  • first is moved to its next element (RED FLAG)
  • temp's data is returned...

One problem is that you're moving your head reference around - this is a bad idea, since it means that you can never access the true first element in your list ever again.

Now on its own, even with this implementation, you should still be able to get the first element.

The above was just a red herring - although you should not be moving your head pointer around. This is the real problem. What happens on subsequent add calls to your list?

public void add(String el) {
    if (isEmpty()) {
        first = new Entry(el);
        last = first;
        return;
    }
    new Entry(el,last);
}

Only the first element inserted and the last element inserted are respected. All other entries after next are overwritten.

I suggest that you use a debugger to figure this one out, as it stems from a misunderstanding of a good approach to do this. You only want to insert things through your tail pointer once you have one element. Doing this through object creation only causes heartache and confusion.

For posterity, I'll leave you with a sample, verbatim implementation I wrote for a singly linked list implementation I did a while back. It describes a more viable approach to inserting into a list.

public void insert(E data) {
    Node<E> candidate = new Node<>(data);

    if(head == null) {
        head = candidate;
        tail = head;
    } else {
        tail.setNext(candidate);
        tail = tail.getNext();
    }
    size = size + 1;
}

Upvotes: 0

Related Questions