I get a NullPointerException calling a Superclass Method in Subclass Inner Class Constructor… What’s the Deal?
In my application’s main class (subclass of Application), I have a public inner class that simply contains 3 public string objects. In the parent class I declare an object of that inner class.
public class MainApplication extends Application {
public class Data {
public String x;
public String y;
public String z;
}
private Data data;
MainApplication() {
data = new Data()
data.x = SuperClassMethod();
}
}
After I instantiate the object in the constructor, I get a runtime error when I try to assign a value in the inner class with a superclass method.
Any idea what’s up here?? Can you not call superclass methods in the subclass constructor?
** Edit ** Original question was about inner class member assignment in outer class constructor. Turned out the issue was with calling a superclass method in the class’s constructor. It was giving me a null pointer exception. Thus, the question has changed.
Try making your innerclass
static:that way it isn’t tied to the MainApplication instance.
update
From your comment it seems that you mean that the
Applicationpart of the object-under-construction is not initialised correctly when methods on it are called.Calling methods of objects that are being constructed from their own constructor can result in unexpected behaviour as the objects are not initialised consistently until the constructor finishes. That said, it is possible that adding an explicit call to the super constructor fixes your dependency: