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 destroy all the states.
public void clearStates()
{
LogFactory.getLog(StateController.class.getName())
.info( "Clearing states. #ofstates="+stateStack.size());
for (State state : stateStack) // Line 132 (see exception)
{
state.destroy();
}
// ...
}
The following exception was trowed:
INFO controllers.StateController : Clearing states. #ofstates=1
java.lang.NullPointerException\
at java.util.LinkedList$ListItr.next(LinkedList.java:891)
at *.controllers.StateController.clearStates(StateController.java:132)
// ... //
This code usually works without a problem and has been in the production for more than a year.
Is it possible that this is Java bug?
/* Update */
destroy() call does not modify stateStack. If it would I guess Java would throw ConcurrentModificationException.
stateStack was populated with 1 state, which overrides destroy, but only does local modifications. The super implementation than prints additional log ("Destroying state..."), which was not in the log file, so I guess the exception was thrown at the beginning of iteration.
public void destroy()
{
destroyed = true;
LogFactory.getLog(State.class.getName()).info( "Destorying state : "+getClass().getName());
propertyChangeSupport.firePropertyChange(PROP_DESTROYED, null, this);
}
:132? Maybestatein your list isnull?state.destroy(). What does it do? Also, how isstateStackpopulated?iterator.next(), rather thanstate.destroy(). You could file a bug reportnull, the list must returnnulland not throw an internal exception. It looks like concurrent modification.