I’m new to both Eclipse and Java.
I’m wondering if the following is an error in Eclipses compiler or my installation.
I have defined a public class inside a public class to define a return type for a service method.
public class ServiceThing {
public class ReturnType {...}
public ReturnType serviceMethod (...) {...}
...
}
In the class where I call the service method I instatiate a ReturnType to hold a default message:
ReturnType returnType = new ReturnType(...);
When a try to build this I get the following errors:
Building workspace:
Errors occurred during the build.
Errors running builder ‘Java Builder’ on project ‘XXXX.android’.
java.lang.ArrayIndexOutOfBoundsException
Save failed:
Save Failed;
java.lang.NullPointerException
I found out that the required syntax is:
ServiceThing serviceThing = ...;
ReturnType returnType = serviceThing.new ReturnType(...);
But the compiler should not generate a nullpointerException anyway.
By making
ReturnTypeapublic static classyou will get rid of the reference to the parent instance and yourwill work as you expected it to.
Without the
staticmodifier instances of subclasses contain an implicit reference to their parent objects. That is the reason why you need aServiceThinginstance to create an instance ofReturnTypein this case.