java - Why my class field is still pointing null after the initialisation? -
i have java nullpointerexception question.
for instance: have 3 classes a, b , c.
public class { public void methoda1() { } } ////////////////////////////// public class b { private a; private c c; public void methodb1() { = c.methodc1; } public void methodb2() { a.methoda1(); } } ///////////////////////////// public class c { public methodc1() { return new a(); } }
as can see code above, main issues in class b. question is, if field in class b initilised in methodb1, why cannot use again in methodb2? compiler giving me nullpointerexception using code a.methodea1(). , cannot create new again need results methodb1(), namely in methodb1(). help, please.
you create object of c
class forget assign new object
public class b { private a; private c c; public void methodb1() { c = new c(); // added = c.methodc1(); // problem here } public void methodb2() { a.methoda1(); } }
you may want read nullpointerexception.
inside constructor must firstly call methodb1()
:
public b(){ this.methodb1(); this.methodb2(); } public static void main(string...args){ new b(); }
because of avoiding nullpointerexception of a
, see inside methodb1 assign new object ot a
.
Comments
Post a Comment