Forums Register Login

null pointer exception by Integer

+Pie Number of slices to send: Send


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 ?
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2899 times.
Similar Threads
Question on K& B chapter 5.
Overriden method throwing Exception.
Exceptions with Overriding
chapter 5,question-8,(k&b)
Exception Handling with Inheritance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:09:30.