开发者

Build error in 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' o开发者_如何学Pythonn 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜