• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why null pointer exception?

 
kshitij kaushik
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 12083
    29
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    kshitij kaushik
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48370
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Sami Devine
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20492
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    String s = null;
     
    Campbell Ritchie
    Sheriff
    Posts: 48370
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please tell me what is wrong with my code tags. Isnt it codebutton-code-codebutton?
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Posts: 15205
    36
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48370
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20492
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic