Why does this code sometimes throw a NullPointerException?

Posted by Tiberiu Hajas on Stack Overflow See other posts from Stack Overflow or by Tiberiu Hajas
Published on 2010-03-16T21:16:47Z Indexed on 2010/04/13 17:33 UTC
Read the original article Hit count: 178

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about hashmap