java - Accessing variables and getting null value -
i'm working on class has inner classes. these classes public , variable passed on other class declared public also. problem whenever, try access example inner class named public class classx
and needing variable public int position
. inside inner class classx
there method calculating position, , in inner class public class classy
, wanted display current value of position
whenever tried access position classx null?how happen?
by way, aware of using instantiate , using constructor. tried set reset value.
here sample code:
public class mainclass { public static class subclass1 { public int position,x,y; } public class subclass2 { private subclass1 s; public void methodcalculate() { s = new subclass1(); s.position = s.x + sy; } } public class subclass3 { private subclass1 s; public void displaymethod() { system.print(" position: ", s.position); } }
}
can please guide me right direction solving problem.
public class subclass3 { private subclass1 s; public void displaymethod() { system.print(" position: ", s.position); } }
s
not initialized anywhere here. value null. obviously, when trying access position field, nullpointerexception. able use object, need have object. in real life: watch tv, need tv:
public class subclass3 { private subclass1 s = new subclass1(); public void displaymethod() { system.print(" position: ", s.position); } }
Comments
Post a Comment