Java Iterator on doubly linked list

不羁岁月 提交于 2020-01-03 06:40:14

问题


Hi I'm very new to Java and have this problem with building a nested Iterator class for a Doubly Linked List. I'm getting this error on E next method when running the test program. The goal of the next method in the Iterator is to return the next item in the Doubly Linked List.

Can anyone advice a fix on my code? Any help is greatly appreciated!

Error message:

Exception in thread "main" java.lang.NullPointerException at dlinkedlist.Deque$DoubleListIterator.next(Deque.java:51)

    public E next() {
        if (!hasNext()) throw new NoSuchElementException();
        last = current;
        E value = current.item;
        current = current.next; 
        index++;
        return value;
    }
    public void remove() { throw new UnsupportedOperationException(); }
  }// end class ListIterator

回答1:


It seems your current object is null. Can you check it?



来源:https://stackoverflow.com/questions/30023620/java-iterator-on-doubly-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!