• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why I'm getting a NullPointerException ?  RSS feed

 
Eugenio Nassu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On this code:



Why I'm getting a NullPointerException ?

((BigDecimal)b).intValue() is being evaluated ? It shound't be, as far I know...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the first branch of the ?: is an int, the second is reified to an int as well -- in other words, code is generated to call intValue() on the Integer you get from b. Then because you're assigning the result to an Integer, it's going to be boxed again. The NPE comes from the "invisible" call to intValue() on b in the second branch, not the explicit one in the first branch.

Now, you might argue that it would be smarter to generate code to box the first branch, and then assign the resulting Integer from either branch to i -- but it doesn't work that way. The type of any left-hand-side variable isn't considered when figuring out how to evaluate the ?: itself. That's clearly something a bytecode optimizer could do, though.
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Because the first branch of the ?: is an int, the second is reified to an int as well


Please correct my understanding if wrong.

The above statement holds true only while assigning a value using :? ternary operator and not during if-else condition check.

And how does compiler come to know if the first branch is actually int without invoking intValue() of the first branch then? Looks like it doesn't actually invoke intValue() but just decides on datatype before jumping onto second branch.

Considering this what actually happens at runtime, it does make sense that in the second branch it would first unbox and box again.

But the basic question is why it is supposed to even go to first branch?

I'm sorry if I sound too much confused.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how does compiler come to know if the first branch is actually int without invoking intValue() of the first branch then? Looks like it doesn't actually invoke intValue() but just decides on datatype before jumping onto second branch.


Easy...



b is an object type. It is casted to the BigDecimal typel. And the intValue() is declared to return an int. The compiler can get all of this information during the parsing of the expression, and from the class files in the class path. It doesn't need to determine the type at runtime.

Considering this what actually happens at runtime, it does make sense that in the second branch it would first unbox and box again.

But the basic question is why it is supposed to even go to first branch?


As mentioned, this is determined at compile time -- it doesn't "go to the first branch". The tenary expression is merely has a type that is determined by the first branch.

And BTW, if you still don't believe that this is happening, you can change the code to this...



And notice that it is stilll happening.

Henry
 
Amol Katyare
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!