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

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!
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic