• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why NullPointerException

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I saw this code in enthuware test. I cant understand why is this program throws NullPointerException, I appreciate the effort of enthuware admin to explain the concept, unfortunately I am unable to follow it. Here is the code,



What will the following class print when compiled and run?

Answer is NullPointerException thrown at the runtime. According to me the answer should be 5 5. Which was not at all in the choice given,
Why I figured out this value, I have tried to explain in the attachment (as I was struggling to put it in words). Please help me solve this.

Thanks in advance,
Prathima
Screenshot (7).png
[Thumbnail for Screenshot (7).png]
Screenshot (8).png
[Thumbnail for Screenshot (8).png]
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because on line 17, calling setIt sets a.link to null (which is value of b.link at that moment). Then on line 18 you are trying to access value field of null object a.link in System.out.println.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because a.link is null if you do this a.link.value you get NullPointerExcepiton
at point x.link = y.link;
y.link=null;
so
x.link = y.link = null;
answer.png
[Thumbnail for answer.png]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:Why I figured out this value, I have tried to explain in the attachment (as I was struggling to put it in words). Please help me solve this.

I'll give it a try. To make explanation easier I added line numbers to all statements that matter. So we can easily refer to them.

If we closely look at the Holder class, we see that each Holder instance has 2 data members: value (an int, can NEVER be null) and link (a reference variable of type Holder, can be null when not assigned a value).

Now let's start with evaluating the main method:
  • 1/ line1: a new Holder instance is created and is referred by a. Data members: [value=5, link=null]
  • 2/ line2: another new Holder instance is created and is referred by b. Data members: [value=10, link=null]
  • 3/ line3: b is assigned to a.link. So data members of object a is referring to: [value=5, link=b]
  • 4/ line4: setIt(a, b) is assigned to b.link. So the next step is to evaluate the setIt call to know the effect on the data members...
  • 4a/ the setIt method is called with x=a (data members: [value=5, link=b]) and y=b (data members: [value=10, link=null])
  • 4b/ lineA: y.link (which is null) is assigned to x.link (which was b). So only data members of object x (a) is referring to have changed: [value=5, link=null]
  • 4c/ lineB: x (a) is returned
  • 4d/ line4: the return value x (a) is assigned to b.link. So data members of object b is referring to: [value=10, link=a]
  • 5/ line5: data members value of a.link and b.link are printed. But if we look at the data members of the object a is referring to ([value=5, link=null]), we see link is null; hence the NullPointerException being thrown


  • Hope it helps!
    Kind regards,
    Roel

    [edit] updated line numbers (Thanks Mushfiq)
     
    Prathima gaitonde
    Ranch Hand
    Posts: 130
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel & Sergej Smoljanov,

    I deeply appreciate your patience and in depth explanation. Apologies for still having doubt in my mind,

    Roel De Nijs wrote:4a/ the setIt method is called with x=a (data members: [value=5, link=b]) and y=b (data members: [value=10, link=null])
    4b/ lineA: y.link (which is null) is assigned to x.link (which was b). So only data members of object x (a) is referring to have changed: [value=5, link=null]
    4c/ lineB: x (a) is returned
    4d/ line8: the return value x (a) is assigned to b.link. So data members of object b is referring to: [value=10, link=a]
    5/ line5: data members value of a.link and b.link are printed. But if we look at the data members of the object a is referring to ([value=5, link=null]), we see link is null; hence the NullPointerException being thrown


    My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
    but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.

    Thanks for being with me through the journey,

    With great respect,
    Prathima
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
    but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.

    Let's get through this step by step. And it's you who has to fill in the blanks (question marks). I'll just guide you.

    What's the situation after line3 is executed?
    1/ Reference variable a refers to a Holder object with data members value = ? and link = ?
    2/ Reference variable b refers to a Holder object with data members value = ? and link = ?

    So fill the question marks with the appropriate values (according to you after line3 is executed). And then we'll continue.

    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
    but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.

    You could also create a print method to see what's the state of the data members. For example:And then in the code snippet simply invoke print when you want to:

    Hope it helps!
    Kind regards,
    Roel
     
    Prathima gaitonde
    Ranch Hand
    Posts: 130
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,

    I kind of figured it out, not sure have I spotted it or totally lost.

    1/ Reference variable a refers to a Holder object with data members value = 5 and link =b.link=null(At this point a.link--->null<---b.link)
    2/ Reference variable b refers to a Holder object with data members value =5 and link =a which intern(b.link.v=5), [ wait a minute, what about b.link.link(which is a.link)?!!! is this null?]

    now, at 1/ Reference variable a.link-->object a<---b.link
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:1/ Reference variable a refers to a Holder object with data members value = 5 and link =b.link=null(At this point a.link--->null<---b.link)
    2/ Reference variable b refers to a Holder object with data members value =5 and link =a which intern(b.link.v=5), [ wait a minute, what about b.link.link(which is a.link)?!!! is this null?]

    No, that's incorrect!

    When line3 is executed, the situation looks like this:
    - Reference variable a refers to a Holder object with data members value = 5 and link = b
    - Reference variable b refers to a Holder object with data members value = 10 and link = null

    Can you see why this is the situation after line3 is executed? Otherwise there is no reason to proceed. You must understand this first!

    Kind regards,
    Roel
     
    Sergej Smoljanov
    Ranch Hand
    Posts: 467
    10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:
    My confusion lies in here, How come link of x(a) remains null? at line 4b/ lineA: a-->[value=5,link=b.link], that means it is referring to b.link object isn't it? Which is at this point null I agree.
    but, when at line 4d/line 8: the return value changes b.link's value which is 'a', a.link will be pointing to object 'a' is it not right? Sorry for not getting your's and Sergej Smoljanov's detailed explanation.

    i think you think (sorry if i can`t guess your mind and make mistake in my opinion )
    that at line

    reference link of object will be reefer to another link, but reference variable can only refer to actual object or null (or may be local not initialized but in this case you cant use it(before you initialize it)).
    you can`t have link to link, you always have link to object or null.

    Example:
    internally reference variable store just some address of object
    also point you to this Pass-by-Value Please (Cup Size continued)
    so at line

    you just copy to x.link address of null (that was stored in y.link)
    and next changes b.link (that is same x.link (inside method) , because b and x point to same object) you change only address of b.link and not change a.link that now is null.
     
    Prathima gaitonde
    Ranch Hand
    Posts: 130
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel & Sergej Smoljanov,

    Thanks for the help, I deeply respect your great work, @Sergej Smoljanov, your last explanation was very clear to me, felt silly to not figure it out at a first glance,

    my understanding:
    1> in method SetIt(), x.link = y.link; at this point a.link--->null<----b.link and also x.link--->same null (object)<----y.link

    2> But when method returns value x, scope of x, y ends, b.link---->a (only) reffernce of b.link is changed.

    3> a.link--->null pointing to original value(object).

    One last thing, can you please teach me, How to write the diagram? that you draw, as your first reply to this post. As I struggle to do that and finally draw it on paper and scanned it.

    With great respect,
    Prathima
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:One last thing, can you please teach me, How to write the diagram? that you draw, as your first reply to this post. As I struggle to do that and finally draw it on paper and scanned it.

    The fun part about drawing these diagrams is, there is no good and bad way. As long as you understand it, it's fine. You simply draw what you see. If a reference variable refers to an object, you can draw an arrow (or something else you want). If the reference variable points to another object (or null), you can erase the arrow or put a cross on it or ... For these kind of exercises it doesn't matter if objects are on the stack or heap.

    Attached you'll find my drawing. I used different colors, otherwise it makes absolutely no sense in just 1 drawing (and I didn't want to scan each step ). But if it were just for me, I would draw this image using just 1 color.

    Hope it helps!
    Kind regards,
    Roel
    npe.jpg
    [Thumbnail for npe.jpg]
     
    Prathima gaitonde
    Ranch Hand
    Posts: 130
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,

    Thanks for the reply. As Sergej Smoljanov, has drawn the picture very neatly I presume he has done using it some kind of a software, I tried it in power point, dint work, hence wanted to know and learn the technique of drawing it neatly. Hand drawing and representation of object, heap, stack representation, I came to know during this journey, all thanks to K&B book.


    With great respect,
    Prathima
     
    Sergej Smoljanov
    Ranch Hand
    Posts: 467
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I use MS Paint just copy paste and some additional drawing
    on exam there is no software - so you`ll use paper and marker.
    not for this exam - i think if you learn UML this be good
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Prathima gaitonde wrote:As Sergej Smoljanov, has drawn the picture very neatly I presume he has done using it some kind of a software, I tried it in power point, dint work, hence wanted to know and learn the technique of drawing it neatly.

    Probably using Microsoft Word and its drawing tools will work too. But as Sergej already mentioned, any (free) drawing program like MS Paint or pant.net will get the job done. Happy drawing!
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    Now let's start with evaluating the main method:
  • 1/ line5: a new Holder instance is created and is referred by a. Data members: [value=5, link=null]
  • 2/ line6: another new Holder instance is created and is referred by b. Data members: [value=10, link=null]
  • 3/ line7: b is assigned to a.link. So data members of object a is referring to: [value=5, link=b]
  • 4/ line8: setIt(a, b) is assigned to b.link. So the next step is to evaluate the setIt call to know the effect on the data members...
  • 4a/ the setIt method is called with x=a (data members: [value=5, link=b]) and y=b (data members: [value=10, link=null])
  • 4b/ lineA: y.link (which is null) is assigned to x.link (which was b). So only data members of object x (a) is referring to have changed: [value=5, link=null]
  • 4c/ lineB: x (a) is returned
  • 4d/ line8: the return value x (a) is assigned to b.link. So data members of object b is referring to: [value=10, link=a]
  • 5/ line5: data members value of a.link and b.link are printed. But if we look at the data members of the object a is referring to ([value=5, link=null]), we see link is null; hence the NullPointerException being thrown


  • I read this post carefully, line5, line6, line7, line8 are consistent line1, line2, line3, line4 respectively. Maybe you intended to write as this, Roel.
    I confuse one point which is regarding pass-by-value. I thought such a.link would still refer b after setIt() method executed. I made some changes to test it with easy way.

    In line1 y is assigned to x but the value of str (which a refers) doesn't change because of pass-by-value. I followed this rule. But I saw that if we put comment line1 and uncomment line2 the output would be b b ...
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mushfiq Mammadov wrote:I read this post carefully, line5, line6, line7, line8 are consistent line1, line2, line3, line4 respectively. Maybe you intended to write as this, Roel.

    Yeah, I probably started with the line numbers of the code snippet and then added my own line numbers (for better clarity), but forgot to change the initial line numbers. Fixed it in the original post (and gave you credit for it )

    Mushfiq Mammadov wrote:In line1 y is assigned to x but the value of str (which a refers) doesn't change because of pass-by-value. I followed this rule. But I saw that if we put comment line1 and uncomment line2 the output would be b b ...

    Did you already have read this excellent article about pass-by-value? If you didn't, definitely do!

    Let's have a simplified version of your code (which will hopefully clear your doubts). First a simple Dog classAnd here is a simple test class to play withIf you run the TestDog class you'll get this output:
    rename: Pluto
    main: Aiko

    And that's probably what you expected as Java uses pass-by-value. So at line0 dog refers to the same object as d. The value of dog (the "address" of the actual object) is a copy of the value of d. At line1 a new object is created and assigned to dog, so now dog has another value than d. And because values are passed by value, this assignment has no effect on the value of d and thus now dog and d refer to two different objects. And that's why you get two different outputs.

    What happens if we remove line1 from the previous code snippet? Let's seeUsing this adjusted method gives a different output:
    rename: Pluto
    main: Pluto

    Once you know what's happening it's really easy So at line0 dog refers to the same object as d. The value of dog (the "address" of the actual object) is a copy of the value of d. So because dog refers to the same object as d, you can still make changes to the object (e.g. changing the name). But changes you make (e.g. assigning to null, assigning to another object,...) to the reference variable itself (in this example dog) have no effect on the value of the original reference variable (in this example d). So pass-by-value is only about variables (primitive or reference), not about the actual objects reference variables are refering to. With reference variables you pass a copy of the reference (the "address" of the actual object), not a copy of the actual object! You never pass objects, only references ("addresses" of those objects).

    And to finish, you can rewrite the above programs using nothing but local variables (you could consider a method parameter as a local variable), which might help a little bit in understanding this concept.This code snippet gives the same output as the first code snippet. And in this code snippet you can simply remove line1 to get the same output as the second code snippet.

    Hope it helps!
    Kind regards,
    Roel
     
    Mushfiq Mammadov
    Ranch Hand
    Posts: 187
    25
    Java jQuery Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your helpful reply, Roel!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic