3

Hi I'm very new to Java and trying to create a Deque class by implementing a doubly linked-list format. When I run the code(DequeApp), I get a NullPointerException refer back to my Iterator.next(Deque.java:44).

Error messages:  **Exception in thread "main" java.lang.NullPointerException
    at dlist.Deque$DoubleListIterator.next(Deque.java:44)



        public E next() {
                if (!hasNext()) {throw new NoSuchElementException();}
                else{
                E temp = current.item;
                current = current.next;
                return temp;}
            }
4
  • possible duplicate of Java Iterator on doubly linked list Commented May 5, 2015 at 8:28
  • Very similar code (there are not that many ways to write linked lists...), @Aakash, although the index is incremented correctly there. Commented May 5, 2015 at 8:32
  • I understand that the algorithm would be same and most implementations would also be same, but the OP has asked same question in 2 different places, with same problem. He is getting NPE for which the solution was already provided. Though I have marked the question as duplicate, I also gave the solution to the problem myself as well. Commented May 5, 2015 at 8:40
  • Yes sorry that i asked the question twice, it's just that I didn't fully understand the other post, sorry I'm only starting to learn java, will hopefully get better at it, thanks Aakash! Commented May 5, 2015 at 8:44

3 Answers 3

3

I have made two changes.

  1. As tucuxi already told, increment index.
  2. Start current from head, not head.next.

    private class DoubleListIterator implements Iterator<E> {
    // instance variable
    private Node current = head;
    private int index = 0;
    
    public boolean hasNext() {
        return index < N;
    }
    
    public E next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        } else {
            index++;
            E temp = current.item;
            current = current.next;
            return temp;
        }
    }
    
    public void remove() {
        throw new UnsupportedOperationException();
    }
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Most welcome. But @tucuxi said right. Build debugging habit and make sure everything is in place where they should be.
2

You forgot to increment your index counter in the DoubleListIterator. You write:

public E next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    } else {
        E temp = current.item;
        current = current.next;
        return temp;
    }
}

And you should have written:

public E next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    } else {
        index ++; // <---- without this, hasNext() always returns true
        E temp = current.item;
        current = current.next;
        return temp;
    }
}

Note also that I have changed the indenting format to that of Oracle's guidelines.

A second error is that you initialize your Iterator as follows:

    private Node current=head.next;

However, this makes it impossible to retrieve head (as you are already pointing to its next node). And it makes you index counter off-by-one. Corrected code:

    private Node current=head;

2 Comments

Hi tucuxi, sorry I'm still getting the same exception. Exception in thread "main" java.lang.NullPointerException at dlist.Deque$DoubleListIterator.next(Deque.java:46) Which is this line "E temp = current.item;"
Found another error further up. You should really do a full pass with a debugger making sure everything looks good; untested code is always full of errors on the first run.
0

Another option to using index variable is

Maybe you could try "current.next != null" within hasNext.

But if it is already working with index no issues then.

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.