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 ReturnType
a public static class
you will get rid of the reference to the parent instance and your
ReturnType returnType = new ReturnType(...);
will work as you expected it to.
Without the static
modifier instances of subclasses contain an implicit reference to their parent objects. That is the reason why you need a ServiceThing
instance to create an instance of ReturnType
in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With