• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question for OCA Assignments

 
Fernando Salgueiro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding to this java snipet :



The result is :
hi
hi
Exception in thread "main" java.lang.NullPointerException
at Mixer.main(Mixer.java:17)


On this line i see Mixer m4 = m3.m1; m4.go(); // and as far as i know Mixer m1; is initialized as null , so m3.m1 should be null and not perform m4.go(); !!!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. m1 is an instance variable. 32's instance of m1 is m2. Which is not null.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor that takes a mixer initializes m1. The other constructor leaves it null. That makes m5 null and m4 not null. Soooo m3,go prints hi and m4.go prints hi but m5.go is called on the null which is m5, so boom down it goes.

 
Fernando Salgueiro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:The constructor that takes a mixer initializes m1. The other constructor leaves it null. That makes m5 null and m4 not null. Soooo m3,go prints hi and m4.go prints hi but m5.go is called on the null which is m5, so boom down it goes.



For me Mixer m1; is null not initialized and is a member/variable from Class Mixer. So

Mixer m4 = m3.m1; == Mixer m4 = m3.null !!! That s is my though !!!
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fernando Salgueiro wrote:
For me Mixer m1; is null not initialized and is a member/variable from Class Mixer. So

Mixer m4 = m3.m1; == Mixer m4 = m3.null !!! That s is my though !!!


This initializes m3's m1 to m2

because it runs this constructor


This initializes m4 to m3's m1 (which is m2)



 
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
Fernando Salgueiro wrote:On this line i see Mixer m4 = m3.m1; m4.go(); // and as far as i know Mixer m1; is initialized as null , so m3.m1 should be null and not perform m4.go(); !!!

With some small changes to the code snippet, you'll see what's actually happening. I didn't do anything special in the adjusted code snippet, I just added some variable names in order to pretty print the Mixer instancesOutput:
m2=Mixer@468864af m2.m1=null
m3=Mixer@248a4156 m3.m1=Mixer@468864af
hi Mixer@248a4156
m4=Mixer@468864af
hi Mixer@468864af
m5=null
Exception in thread "main" java.lang.NullPointerException
at Mixer.main(Mixer.java:17)


So m4 is clearly not null, but m5 definitely is!

Another tip which might be useful for questions like this (and "how many objects are eligible for garbage collection" question), both whyile studying and on the actual exam: it really helps to make just a simple drawing of the code. Examples here, here and here.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic