0

Just like the title says, I am getting a null pointer exception whenever I try to call the iterator from my driver. The error points to this specific location in my class:

 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();
    }
}

}

Why am I getting a NPE?

Orcka
  • 191
  • 1
  • 2
  • 7

0 Answers0