就像标题所说的那样,每当我尝试从驱动程序调用迭代器时,我都会得到一个空指针异常。错误指向我班级中的这个特定位置:
public Iterator<E> iterator() {
return new IteratorHelper();
}
class IteratorHelper implements Iterator<E> {
private int iteratorIndex;
private long modCheck;
Node<E> current;
public IteratorHelper() {
iteratorIndex = 0;
modCheck = sequenceNumber;
current = head;
}
public boolean hasNext() {
if (modCheck != sequenceNumber)
throw new ConcurrentModificationException();
return iteratorIndex < currentSize;
}
public E next() {
if (!hasNext() && current != null)
throw new NoSuchElementException();
iteratorIndex++;
current = current.next;
return current.data; //NPE <<<<<<<<<<
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
为什么我要获得NPE?