Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception by Integer

 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I took this code from SUN's assessment exam and modified it a little

return Integer.getInteger(arg); I want to ask why this line gives null pointer exception ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what the method Integer.getInteger() does? It most likely does not do what you think it does.

The API documentation says:
Determines the integer value of the system property with the specified name.

If there is no system property named "42", then Integer.getInteger() returns null, and because Parser.getInt() returns a primitive int, the JVM is trying to auto-unwrap null to an int - which causes a NullPointerException.

It's a trick question...
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check the use of getInteger() method then you would get an answer to your question.
Looking at the code it seems you are better off using valueOf() or parseInt() method of Integer class.
 
Tuna Töre
Ranch Hand
Posts: 220
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeass

very tricky, actually question is not like that I have modified it and see the null pointer exceltion, thanks I got it



The real question is like above and works fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic