I couldn’t resist creating this very short, dorky and entirely programming-focused entry today. If you are not a software engineer, read no further. You have been warned.
I recently ran across a subtle way a handy Java 1.5 feature can sneak up and
cause issues. Perhaps if I had spent more time pouring over the specs &
documentation, it would’ve been very obvious, but alas, it definitely caught
me by surprise. The problem was a NullPointerException
(NPE) which I tracked
down to the following line of code:
The obvious culprit was that HashMap
named map was null
, but as I stepped
through the code, I found to my surprise that it wasn’t. Neither was key, for
that matter. So how the hell was I getting an NPE? Take a look at the
declaration of map
:
Note how the values in map are Integer
objects, but I’m setting the result of
the get
call to a primitive int
. In Java 1.5, this is allowed as Java will
automatically unbox the Integer
into an int
. But what happens if the value
returned by get
is null
? Well, the auto unboxing can’t convert that to any
int
value—returning some default value like 0 or -1 would be very
deceiving—so you get a big old NPE.
Moral of the story: auto boxing and unboxing are very handy features to keep your code clean and readable, but keep NPE’s in mind every time you use them.
Yevgeniy Brikman
If you enjoyed this post, you may also like my books. If you need help with DevOps, reach out to me at Gruntwork.