| Author |
why null pointer exception?
|
kshitij kaushik
Greenhorn
Joined: Jan 14, 2009
Posts: 11
|
|
guys I was going through Head First Java's sharpen your pencil exercise...on compiling the code below,
I am getting Null Pointer Exception error during run time...why it is so?
although on initiating "i"...it is running properly. For example
while doing the same for "j"...still null pointer exception...this whole stuff of auto boxing is not clear to me....please tell me...
|
thanx
kshitij
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
kshitij kaushik wrote:
although on initiating "i"...it is running properly. For example
while doing the same for "j"...still null pointer exception...this whole stuff of auto boxing is not clear to me....please tell me...
So you figured out the problem your self.
Instance variables, if not initialized, gets 'null' object (OR get default value in case of primitive data type).. And you cant perform any operation on "null" object, so NPE.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
Quite right: you are trying to get the value out of the null Integer field. You are actually trying to un-box the null value.
There are however a few things you can do with a null value:You can assign it, so it isn't null any more.You can assign null to another identifier, so it does now point to null.You can test for equality and inequality, eg "if (field == null) . . ." "if (field2 != null) . . ."
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9942
|
|
i think it's the j=i line that would throw the exception. since j is an int, it only holds a value, not an object. so, when you try to assign i to j, you are basically doing this:
j = i.intValue();
but since i is null, you get your NPE.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
kshitij kaushik
Greenhorn
Joined: Jan 14, 2009
Posts: 11
|
|
thanks sagar you helped me to find out mine mistake.....
thanks campbell you have given me a great source of information......
thanks fred you have pointed very correctly....I checked by changing both the instance variable type to "Integer"
and "int" as well......now it is not giving any run time exception.....instead of that its giving as expected "null" and "0" as output...
once again thanks a lot to all of you guys........
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
You're welcome
|
 |
Sami Devine
Ranch Hand
Joined: Jan 30, 2011
Posts: 44
|
|
Campbell Ritchie wrote:Quite right: you are trying to get the value out of the null Integer field. You are actually trying to un-box the null value.
There are however a few things you can do with a null value: You can assign it, so it isn't null any more.You can assign null to another identifier, so it does now point to null.You can test for equality and inequality, eg "if (field == null) . . ." "if (field2 != null) . . ."
Hi Campbell,
I tried out the same excercise.
What do you mean by "You can assign null to another identifier, so it does now point to null"?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
String s = null;
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
Rob Spoor wrote:String s = null;
. . . or . . . orAll those will set both s and ss to null. It shows how easily nulls can propagate through your code
|
 |
Sami Devine
Ranch Hand
Joined: Jan 30, 2011
Posts: 44
|
|
Thanks Campbell, Rob.
I tried a few things. I tried the following and got NPE. Shouldn't this work for Integer also as it is an object?
|
 |
Sami Devine
Ranch Hand
Joined: Jan 30, 2011
Posts: 44
|
|
|
Please tell me what is wrong with my code tags. Isnt it codebutton-code-codebutton?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
You're probably double-clicking the code button, so that your text looks like:
[code][/code]
.... some code here ....
[code][/code]
Intead, you should just click the code button once to add a "code open" tag [code] and at the end click it once again to add a "code close" tag [/code].
Note: you can also manually type in the [code] and [/code] tags instead of using the button.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32631
|
|
Jesper de Jong wrote:You're probably double-clicking the code button, so that your text looks like:
[ code][ /code]
.... some code here ....
[ code][ /code] . . . .
Yes, it does look like that, so I went and corrected it. Unfortunately the code wasn't indented, so the code tags didn't do very much.
It is actually confusing that there isn't an obvious space between the start and end code tags.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
k is never assigned a value so it remains null.
i only gets one assignment, but it's with the value of k which is null.
The assignment to j tries to auto-unbox i, but that's still null. That's why you're getting an NPE.
|
 |
 |
|
|
subject: why null pointer exception?
|
|
|