Why does this code sometimes throw a NullPointerException?
- by Tiberiu Hajas
Consider the following Java source:
if( agents != null ) {
  for( Iterator iter = agents.keySet().iterator(); iter.hasNext(); ) {
    // Code that uses iter.next() ...
    //
  }
}
The agents is a HashMap.
Why does the for statement sometimes throw a NullPointerException?
Thank you.