Friday, March 24, 2006

Java Tip #9 - unboxing and the elusive npe

Be aware that unboxing can cause a NullPointerException.
Suppose I have this function.

public void test(int a) {

and I declare this variable
  Integer x = null;
then when I make this call
I get a NullPointerException as x is "unboxed" to an int. A more specific exception such as "NullPointerExceptionDueToUnboxingYouMoron" would have helped.