This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can someone explain why following code will throw npe  RSS feed

 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am not understanding why following code will throw npe, can someone explain.



Thanks
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, please UseCodeTags (⇐Click) when posting code samples. That makes them much easier to read.

You're seeing a weird artifact of autoboxing. Because Integer.parseInt() returns an int, Java decides the whole expression should have the type int, so it tries to unbox t.abc. Unboxing a null will give you an NPE every time. If you changed Integer.parseInt(strA) to Integer.valueOf(strA), which returns an Integer, then the whole expression would be of type Integer. In that case, Java would not try to unbox t.abc, and everything would work fine.
 
Padma Asrani
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for explanation, my problem is resolved after I changed it to a Integer.valueOf()

Thanks
Padma

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!