Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AutoBoxing Integer to int conversion

 
Praveen Palaniswamy
Greenhorn
Posts: 3
Java Mac OS X Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's in the Java™ Language Specification . Look what it says about null on that page.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To show in code what Wouter explained, your line of code is the same as this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic