Bug 15087 - NPE when methods from the outermost enclosing class is invoked in a anonymous class
Summary: NPE when methods from the outermost enclosing class is invoked in a anonymous...
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: 2.0 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-02 10:02 EDT by Olivier Thomann CLA
Modified: 2002-05-06 11:43 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2002-05-02 10:02:45 EDT
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.
Comment 1 Randy Hudson CLA 2002-05-02 11:45:28 EDT
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.
Comment 2 Philipe Mulet CLA 2002-05-04 05:37:11 EDT
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
Comment 3 Philipe Mulet CLA 2002-05-06 11:43:40 EDT
Closing