• 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

Clarification on Q6 Self-Test Chapter 3 K&B SCJP5

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


The answer is: hi hi followed by an exception.
I am not clear on how this is so.
After line 2 are both m2 and m1 pointing to the same object?
line 3 is executed and prints "hi".
line 4 assigns m1 to m4. At this point m2, m1 and m4 are pointing to the same object on the heap. correct?
line 5 is executed and prints "hi".
line 6 assigns m1 to m5 . At this point m2, m1, m4 and m5 are pointing to the same object.
Then line 7 is executed. Is at this point that an exception is thrown?
The explanation given by K&B merely mentions that "m2 object's m1 instance variable is never initialized so when m5 tries to use it a NullPointerException is thrown."
In that case why did we not get a NullPointerException at
Line 4 Mixer m4 = m3.m1;
itself because m1 is null here as well?
Thanks,
Meera


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

meera kanekal wrote:

In that case why did we not get a NullPointerException at
Line 4 Mixer m4 = m3.m1;
itself because m1 is null here as well?

Thanks,
Meera


Hi Meera,

The reason why you don't get an exception at line 4 is because m3.m1 is not null, since the object m3 points to was instantiated (at line 2) using the constructor that takes a Mixer object, with m2 as the argument. After line 2, m3.m1 points to the same object as m2.
But when the object that m2 points to was instantiated (line 1) the no-arg constructor was used, so that left m2.m1 with a default value of null.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 4 should read "Mixer m1;"
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Ruben. This question is clear for me now.
Meera
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent, Meera!
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for barging in here, but whilst reproducing this in Eclipse, I get a compiler error from:



In my opinion, quite rightly so, where is this m1 variable being declared and then called from, on m3??

To give the benefit of the doubt for a possible typo, on the instance variable for the Mixer class did you intend to write:



instead of



if you did then, it compiles: Plese use the preview function to check mistakes before posting or use edit to correct once posted (like i did )




Looking at the code, and running it I get the nullPointerException where m5.go() is called. m5 is pointing to m2.m1. However m2 was created with a no-arg constructor and then used to construct m3. So providing my assumption about m being m1 was correct, there is no instance of m2.m1 to call go on, and hence the nullPointerReference.

?
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corrected Code
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup thats what I thought, I also had a look at the question myself
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic