• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with some code I can not understand

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

One of the questions in Enthuware was what does the following code print:



The answer is that in line //5 it throws a null pointer exception.

I really cannot understand at all this question.
Could anyone help??
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread might be helpful:
http://enthuware.com/forum/viewtopic.php?f=2&t=1134
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get yourself a pencil and paper and go through it step by step keeping a note of the field values as you go. What is the default value for an Object field? For example: What will Holder link be initialised as when the object is constructed?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ioanna Katsanou wrote:I really cannot understand at all this question.
Could anyone help??


This topic contains an excellent explanation with lots of code snippets about exactly the same question. Definitely worth reading!
 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll admit that this one took me a bit to figure out as well. I did look at the other thread mentioned but the statement in the last post 'The method "setInt" does not even get executed' is incorrect. Here is the way I determined the answer:

On line 2, a new instance of Holder is created and assigned to the reference variable 'b'. Note that at this point, the value of b.link is null since nothing has been assigned to it yet
On line 4, the setIt method is invoked. Inside this method, the following statement is executed:

This is where the value of a.link (referred to in this method as x.link) is set to null. It is because b.link (or y.link in this method) is null at this point. Although b refers to a valid object, the b.link property has not been assigned anything at this point
The return value of the setIt() method is the a reference to the holder object 'x' (which is pointing the same object as the reference variable 'a') which is then assigned to b.link (so now b.link has a value but a.link still does not)

On line 5, the statement tries to invoke a method on a null object (b.link) and throws a NullPointerException



 
reply
    Bookmark Topic Watch Topic
  • New Topic