-1
    public class   List_manager<E> {
        Entry<E> first;
        Entry<E> last;
        public void add(E element) {
            Entry<E> e=new Entry(element,last);
            if (first==null) first=last;
        }
        public E get() {
            Entry<E> temp=first;
            first=first.next;
            return temp.data;
        }
        public boolean isEmpty() { return first==null; }


        private static class Entry<E> {
            Entry<E> next;
            E data;
            public Entry(E element,Entry<E> to) {
                data=element;
                next=to;
                to=this;
            }
        }
    }

//now the main class

Trying to make a linkedlist

public class Main {
    public static void main(String[] args) {
        Point p1=new Point(0,0); // 
        Point p2=new Point(12,5);
        Point p3=new Point(43,12);
        List_manager<Point> l=new List_manager<Point>();
        l.add(p1);
        l.add(p2);
        l.add(p3);
        System.out.println(l.get()); // here is an error occurs
        System.out.println(l.get());
    }
}

// Point

Just a simple point

public class Point {
    double x; 
    double y;
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public String toString() {
        return "("+Double.toString(x) + " , " + Double.toString(y) + ")";
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
Anarantt
  • 289
  • 1
  • 2
  • 10

3 Answers3

2

In add method you do:

if (first==null) first=last;

But last is never initialized. It's allways null an thus first is allways null.

When you call get method, yo do:

first=first.next;

As first is null you get the NullPointerException

Look a this part of your code:

public Entry(E element,Entry<E> to) {
    data=element;
    next=to;
    to=this;
}

the last sentence to = this does nothing. You are not modifying last as it seems you expect.

EDIT ---

You should update last in add method:

public void add(E element) {
    Entry<E> e = new Entry(element, null);
    if (last != null) { last.next = e; }
    last = e;
    if (first == null) first = last;
}
Tobías
  • 6,142
  • 4
  • 36
  • 62
  • And why if we assign last in add method it works, but when we pass it to the Entry it become null? Or am i missing smt? – Anarantt Nov 29 '14 at 00:36
  • That's because in Java variables are references but doesn't pass method arguments by reference, it passes them by value. So when you modify `to` you change the object referenced by `to` but `last` is unchanged. In this answer you can find a good explanation: http://stackoverflow.com/a/40523/3963330 – Tobías Nov 29 '14 at 00:56
  • But to refers to the same object as last. – Anarantt Nov 29 '14 at 01:32
  • in add method() Entry e=new Entry(element,last) we pass reference to the object that is being pointed by last and to takes it ... ??? – Anarantt Nov 29 '14 at 01:34
  • Initially `last` and `to` _references_ the same object but they are _different_ _variables_. When you do `to = this` then they do not reference the same object anymore. You are not modifying the referenced object. You are modifying the value of variable `to` so it references another object. Variable `last` keeps unchanged. – Tobías Nov 29 '14 at 02:28
  • You're welcome! if this has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and may help others to find the solution. There is no obligation to do this. – Tobías Nov 29 '14 at 02:56
0

Last is not being set. It will always be null. So first will always be null. When you call get here:

public E get() {
   Entry<E> temp=first;
   first=first.next;
   return temp.data;
}

You are trying to grab the next value from a null object.

OhDearMoshe
  • 39
  • 1
  • 5
0

Your variables last and first has never been initialised:

Entry<E> first;
Entry<E> last;

So in your get() method you will catch a nullPointerException in this line:

first=first.next;
cнŝdk
  • 31,391
  • 7
  • 56
  • 78