Forums Register Login

AutoBoxing Integer to int conversion

+Pie Number of slices to send: Send
int a = (Integer) null;

throws a null pointer exception. Is this documented in javadoc? also if any one could point out why is it not assigned 0 which will be the default for an int.
+Pie Number of slices to send: Send
Hi and welcome to the Javaranch.

What happens is that you have an Integer reference with you want to convert into an int value. This happens by calling the intValue() method on the Integer reference.
But because the Integer reference is null it results in a NullPointerException.
1
+Pie Number of slices to send: Send
It's in the Java™ Language Specification . Look what it says about null on that page.
1
+Pie Number of slices to send: Send
To show in code what Wouter explained, your line of code is the same as this:

Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 20963 times.
Similar Threads
Session question
instanceof operator.
Unable to deploy an EJB.
Wrappers and Autoboxing
overloaded func, both accept same no and type of parameters
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:32:05.