• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

null pointer exception by Integer

 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic