0

This is my first post here. I am trying to create a singly link list. I am using AtEnd and AtStart methods to insert values at the end or in the beginning of the list and using display method to print all the values. The insertion methods seems to be working fine (at least I think so) but whenever I call display method it shows only the first value and then there is a null pointer exception. For example when I run this code I see only 9 and then there is the NPE despite the fact that I have put a check on the display method for "not null".

class node {
    private int data;
    private node next;

    node() {

    }

    node(int data) {
        this.data = data;
        this.next = null;
    }
    public int getData() {
        return data;
    }

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

    public node getNext() {
        return next;
    }

    public void setNext(node next) {
        this.next = next;
    }
}

public class list extends node {
    node head;
    list() {

    }

    public void AtStart(int val) {
        node n = new node(val);
        if (head == null) {
            head=n;
        } else {
            n.setNext(head);
            int temp = head.getData();
            head.setData(val);
            n.setData(temp);
            //n = head;
        }
    }

    public void AtEnd(int val) {
        if (head == null) {
            node n = new node(val);
            head = n;
    } else {
            node t = head;
            for(; t.getNext() != null; ) {
                if(t.getNext() == null) {
                    t.setNext(new node (val));
                }
                t = t.getNext();
            }
        }
    }

    public void display() {
        node t = head;
        for(; t.getNext() == null;) {
            if (t !=null) {
                System.out.println(t.getData());
                t = t.getNext();
            }
        }
    }
}


public static void main(String args[]) {
    list l = new list();
    l.AtStart(16);
    l.AtEnd(6);
    l.AtEnd(36);
    l.AtStart(9);
    l.AtEnd(22);
    l.display();
}
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
jungian
  • 11
  • 1
  • 2
    I'm assuming this is Java. Please tag it with the language, so we know what you're talking about. Also, instead of apologizing for the poor indentation, you should just fix it!! – Jonathon Reinhart Apr 07 '13 at 18:20
  • Furthermore, there have been hundreds, if not thousands, of questions asked (and closed) about `NullPointerException`s because they all boil down to the same solution: **Debug your code**. That's a part of development you simply *have* to learn. See also [**What is a Null Pointer Exception?**](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Jonathon Reinhart Apr 07 '13 at 18:23
  • 1
    Please take time to format your code, instead of posting lame text with excuses. – oleksii Apr 07 '13 at 18:27

1 Answers1

0

i dont get what your AtStart function does, it should be much simpler:

public void AtStart(int val){
if(head==null){
head=n;
}
else{
head.setnext(head);
head.setData(val);
}
}