wood burning stoves*
The moose likes Beginning Java and the fly likes AutoBoxing Integer to int conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "AutoBoxing Integer to int conversion" Watch "AutoBoxing Integer to int conversion" New topic
Author

AutoBoxing Integer to int conversion

Praveen Palaniswamy
Greenhorn

Joined: Feb 09, 2011
Posts: 3

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38066
    
  22
It's in the Java™ Language Specification . Look what it says about null on that page.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

To show in code what Wouter explained, your line of code is the same as this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AutoBoxing Integer to int conversion
 
Similar Threads
Session question
instanceof operator.
overloaded func, both accept same no and type of parameters
Unable to deploy an EJB.
Wrappers and Autoboxing