• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Exception

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Mixer{
Mixer(){
}
Mixer(Mixer m){
m1 = m;
}
Mixer m1;
public static void main(String ar[]){
Mixer m2 = new Mixer();
Mixer m3 =new Mixer(m2);
m3.go();
Mixer m4 =m3.m1;
m4.go();
Mixer m5 = m2.m1;
m5.go();
}
void go(){
System.out.println("hi");
}
}

i thought the answer would be hi,hi,hi
but it gives a nullpoint Exception and hi,hi.
how come is that?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because m2 hasn't has m1 initialised?
 
Craig Bayley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In more detail. M2 is created with a blank constructor so m1 within m2 is the default value, null. Specified but not initialised.
M5 is then created as a reference to M1 inside M2, but this is still null, so when you try call a method on the null object - SPLAT!
 
Let's go to the waterfront with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic