1

I am new to generics and I am trying out to make a linked list.

Here is the Code for Node_ class.

public class Node_<T> {

    private int index;
    private T data;
    private Node_<T> next;
    public Node_() {
    }

    public Node_(T data, int index) {
        this.data = data;
        this.index = index;
        next = null;
    }

    public void set(T data, int index) {
        this.data = data;
        this.index = index;
        this.next= null;
    }

    public void display() {
        System.out.println(this.data.toString());
    }

    public void setindex(int index) {
        this.index = index;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Node_<T> getNext() {
        return next;
    }

    public boolean hasNext() {
        if (next != null) return true;
        else return false;
    }

    public int getIndex() {
        return this.index;
    }
}

and linked list class.

public class LinkList<T> {    
    private int total = 0;
    private Node_<T> start;
    private Node_<T> end;
    private Node_<T> ptr;

    public LinkList() {
    }    

    public LinkList(T data) {
        start = new Node_<T>(data, 0);
        end = start;
        ptr = start;
        total++;
    }

    public void add(T data) {
        if (start == null) {
            start = new Node_<T>(data, 0);
            end = start;
            ptr = start;
            total++;
        } else {
            end.set((T) data,(int) total);
            total++;
            end = end.getNext();
        }
    }


    public void displayAt(int index) {
        if (start != null) {
            ptr = start;
            do {
                if (ptr.getIndex() == index)
                    ptr.display();
            } while (ptr.hasNext());
        }

        else
            System.out.println("No Element found");
    }

    public void displayAll() {
        if (start != null) {
            ptr = start;
            do {
                ptr.display();
            } while (ptr.hasNext());
        }

        else
            System.out.println("No Element Present");
    }
}

The following code in Main class

public class Main {
    public static void main(String[] args) {    
        LinkList<Integer> list = new LinkList<Integer>(25);
        list.displayAll();
        for (int i = 0; i < 11; i++) {
            list.add((Integer) i);
        }
        list.displayAll();
    }

}

I am getting the following error and i cant figure out the problem.

25
Exception in thread "main" java.lang.NullPointerException
at LinkList.add(LinkList.java:26)
at Main.main(Main.java:8)

Any suggestion where am I going wrong.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 7
    This has nothing to do with generics. – Sotirios Delimanolis Mar 16 '14 at 17:26
  • 2
    Use an IDE and step through your code. – Tarik Mar 16 '14 at 17:28
  • 1
    @user3426273 Stack trace indicates that something on the left side of a `.` is `null` in `LinkList.add()`. Looking at the code (and guessing since you did not mark the indicated lines in the snippet) the only thing that could satisfy those conditions is if `end` is `null`. So, find out why and fix that. – Jason C Mar 16 '14 at 17:32
  • @KevinBowersox `end.set(...)` would be the source if `end` were `null`, as it happens first. – Jason C Mar 16 '14 at 17:34
  • LinkList.add(LinkList.java:26) read line 26. – Marco Acierno Mar 16 '14 at 17:34
  • @JasonC this source file may have two extra lines if it has a package declaration and a extra blank line. – Mauren Mar 16 '14 at 17:36
  • @Mauren I don't understand why you are telling me this, sorry. – Jason C Mar 16 '14 at 17:37
  • `Node_` and `LinkList` both have constructors that leave fields null. You might start by fixing that. – Chris Martin Mar 16 '14 at 17:37
  • @JasonC your prev comment answering Kevin Bowersox. – Mauren Mar 16 '14 at 17:38
  • @Mauren Thanks for clarifying. No it was unrelated to line numbers (we don't actually know what the line numbers are). If you follow the code and presume `end` is `null`, it *must* throw the NPE on the `end.set(...)` line. It could not get to `end.getNext()` as `end.set(...)` would throw first. It's not really an issue of the specific line numbers. – Jason C Mar 16 '14 at 17:40
  • @JasonC yes, it was what I diagnosed as well. After counting lines I just presumed line number was wrong, because of this assumption: if `end` is `null` the NPE would be thrown two lines prior. – Mauren Mar 16 '14 at 17:41
  • 2
    @JasonC You are right it is on that line during the second iteration. Since end is assigned to the return value of `getNext` which is null during the first iteration. – Kevin Bowersox Mar 16 '14 at 17:44
  • @KevinBowersox that makes a lot of sense. – Mauren Mar 16 '14 at 17:48
  • 2
    @Mauren I'm so thankful for debuggers! – Kevin Bowersox Mar 16 '14 at 17:55

2 Answers2

3

Cause of NullPointerException

The method Node_.set() leaves the value of end.next null. This means that the after you've been around the loop once you get a NullPointerException.

Corrected add(Node_<T>) method

It appears that rather than adding a new node to the list you are setting the contents of the last node. I think you should be constructing a new node, setting end.next to point to this new node and, finally, setting end as the new node.

public void add(final T data) {
    if (start == null) {
        start = new Node_<T>(data, 0);
        end = start;
        ptr = start;
        total++;
    } else {
        end.setNext(new Node_<T>(data, total));
        total++;
        end = end.getNext();
    }
}

It should be noted that you'll need to update the Node_ class to provide a setter for the next variable - at the moment it is initialised to null and cannot be changed.

Fix for displayAll() method

And finally, once you do create a LinkedList your displayAll routine next moves along the list, if there are multiple elements it just prints the first element forever. You need to include a ptr = ptr.getNext(). Something like this works:

public void displayAll() {
    if (start != null) {
        ptr = start;
        while (ptr != null) {
            ptr.display();
            ptr = ptr.getNext();
        }
    } else {
        System.out.println("No Element Present");
    }
}
Richard Miskin
  • 1,260
  • 7
  • 12
0

When the LinkedList is instantiated it creates a new Node and assigns it to start, end and ptr. When the new Node is created next is assigned null:

public Node_(T data, int index) {
    this.data = data;
    this.index = index;
    next = null; //assigned null
}

The add method is then called repeatedly within the body of the loop. During the first iteration, end is assigned the return value of getNext which is null. On the second iteration a method on end is invoked causing a NullPointerException is thrown.

public void add(T data) {
    if (start == null) {
        start = new Node_<T>(data, 0);
        end = start;
        ptr = start;
        total++;
    } else {
        end.set((T) data,(int) total); //end is null on second iteration
        total++;
        end = end.getNext(); //Set to null on the first iteration
    }
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189