I came across very odd Java behaviour, and I don\'t know if it is a bug, or am I missing something.
The code simply goes through the stateStack (LinkedList) list and
This is the internal implementation of LinkedList.ListItr.next()
:
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next; // your stacktrace says the NullPointerException happens here
nextIndex++;
return lastReturned.item;
}
The NullPointerException
happens because the internal variable next
is null
; however, it seems that hasNext()
is validating that there is a next element.
It seems to me that:
destroy()
while iterating over the list.If you update your answer with your implementation of destroy()
as sugested by @mthmulders, I either update, correct or delete my answer.