0

Node class

private class Node<E> {
    E data;
    Node<E> next;

    public Node(E obj) {
        data = obj;
        next = null;
    }
}

insert method (ascending order)

public void insert(E obj) {
    Node<E> newNode = new Node<E>(obj);
    Node<E> prev = null, curr = head;
    while(curr != null && ((Comparable<E>)obj).compareTo(curr.data) >= 0) {
        prev = curr;
        curr = curr.next;
    }
    if(prev == null) 
        head = newNode;
    else {
        prev.next = newNode;
        newNode.next = curr;
    }
    currentSize++;
}

remove method

public E remove() {
    if(isEmpty())
        return null;
    E tmp = head.data;
    head = head.next;
    currentSize--;
    return tmp;
}

I get Null Pointer Exception at the line

E tmp = head.data;

in the remove method

The error is fixed if the change the else statement in my insert method to

else
    prev.next = newNode;
newNode.next = curr;
4
  • You should include your stack trace. It seems that head would be null, but I'm not seeing which class that is a member of. Commented Oct 19, 2014 at 19:50
  • Sorry head is apart of my linked list class. the node class is an inner class of the linked list class. I don't understand why head would be null because of that else statement though. Also what is a stack trace? Commented Oct 19, 2014 at 19:53
  • how the list look like when you encounter the error ? Commented Oct 19, 2014 at 19:56
  • Does this happen if you insert something into list and then twice call remove? Because you are not setting up ".next" when inserting first element. When you remove him, this nonexistent ".next" is assign to head, so you get NPE if you call remove for the second time. Commented Oct 19, 2014 at 20:05

1 Answer 1

1

There's a problem when you insert a new node that needs to go at the beginning of the list (because it's smaller than the current head node). When you get to this part:

if(prev == null) 
        head = newNode;

you're setting head to be the new node you've just created, but you also need to set newNode.next to be the previous head. So you really want

if(prev == null) {
        newNode.next = head;
        head = newNode;
}

which inserts the new node at the beginning but tacks the previous head onto it.

With your code as it stands, when you add a second element that should go at the beginning, you're accidentally discarding the element that's already there, but you're still increasing currentSize; so you end up with a list with only one element, but a currentSize of 2. When you then try to remove two elements, the second one fails with a NullPointerException because you try to read the data inside a non-existent element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.