Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build error in eclipse

Tags:

java

eclipse

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.

like image 588
Jesper Bischoff-Jensen Avatar asked Oct 10 '22 06:10

Jesper Bischoff-Jensen


1 Answers

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.

like image 111
rsp Avatar answered Oct 13 '22 00:10

rsp