Recent Posts

Archives

Topics


« | Main | »

Integer to int null pointer exception?

By Jeremy | March 20, 2009

As I was coding up some stuff for class today I was using your friendly HashMap in Java.  I created the hash map so it would use an Object and an Integer. I would have rather done an int but you can’t template ints.  Anyway, at one point I was pulling the Integer out of the map with something like this:

int parent = map.get(parentObj);

There happened to be a case where the parent object wasn’t in the map and that line kept throwing a null pointer exception. After a few minutes, I realized that is because the conversion from Integer to int implies an implicit method call. And when the value returned from .get was null, there was a null pointer exception on the Integer.toInt() method. So beware of implicit calls.

Topics: bugs, school | No Comments »

Comments

You must be logged in to post a comment.