Info

Ben is Vice President of Mobile Engineering at Walmart.com, where he works closely with his long-time friend Dion Almaer.

Among the wonderful changes in Java 5 that make code easier to read and maintain (cough) is autoboxing. Consider the following line of code:

return handleIncorrectType(message, modelColumn, o, keyboard);

It was throwing a NullPointerException (not passing one from the underlying method invocation). I had to scratch my head for quite a few minutes to see how it would, as there was no invocation on the parameters that could cause it.

The answer was that one of the parameters was a primitive wrapper type (Boolean) set to a null value, and the autoboxing mechanism punts when coalescing wrapper nulls to primitive types. Fun!

Advertisement

Comments

2 Comments

Post a comment
  1. March 17, 2007

    What would you rather it did?

  2. March 17, 2007

    Perhaps throw something like a WrapperNullException with a message like “A primitive wrapper type contained a null value and could not be coerced to a primitive.”

    Nah, that would be too clear. Better to throw an NPE with no specific explanation.

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

Follow

Get every new post delivered to your Inbox.