Community
Participate
Working Groups
From Eclipse Corner: I have a constructor that creates a an anonymous inner class inside an anonymous inner class. The inner-most class calls a method on the outer-most enclosing instance. When this call occurs (which happen well after construction finished), I get a null pointer exception. This is impossible, since it is just a straight method call with no parameters on the enclosing enclosing this, which cannot be null. When I step into it, it seems that the generated accessor for the enclosing type is returning null, but I can't be sure. public FigureCanvas(Composite parent, int style) { super(parent, checkStyle(style)); lws = new LightweightSystem(this){ //Anonymous class 1 protected RootFigure createRootFigure(){ return new RootFigure(){ //Anonymous class 2 public void validate(){ layoutViewport(); //NULL_POINTER reported at this line number. I cannot step into this method. super.validate(); readjustScrollbars(); } }; } }; lws.setContents(getViewport()); init(); } I am unsure which build is used.
It turns out that the anonymous constructor being called: new LightweightSystem(Canvas){} would result in validate being called during the construction. I suppose the anonymous constructor "new LightweightSystem(this)" get's turned into: new LightweightSystem(Canvas canvas, FigureCanvas enclosingRef){ super(canvas); //super's constructor indirectly results in reference to field set below this.enclosingRef = enclosingRef; } And THEN, the first line calling the super(canvas) constructor eventually results in an access to enclosingRef before it is set. This is causing the NullPointerException. Is there any way to set the enclosingRefence before calling super()? Anyway, I've worked around this already. I suppose this is WAD.
Indeed any super constructor call MUST (vm spec) be invoked before assigning this$N fields used for innerclass emulation. This results in this issue, if the super constructor will dynamically invoke code on the originating subclass which requires to reach into its enclosing type. Javac has the same issue. This is a limitation of Java, not from our compiler. Closing
Closing