mardi 9 juin 2015

Reference is still null when init method returns [duplicate]

This question already has an answer here:

Lately, I observed a strange effect that I do not understand: I'll pass a CTabItem and a Composite to a method. The method will init the composite and set it as the tab's control. This works fine, since composite != null after calling the constructor. However, when my code returns from the method, this.cmpTest == null is still true.

Since I do pass a reference to the method, I would expect the object it to be != null afterwards.

Why isn't this the case?

This is my init method:

private <T extends MyParentClass> void initTab(final CTabItem tabItem, Composite composite, final Class<T> clazz) {
  if (null == composite) {
     try {
        // composite == null
        composite = (T)clazz.getConstructor(Composite.class, int.class).newInstance(this.tabFolder, SWT.NONE);
        tabItem.setControl(composite);
        // composite != null
     }
     catch (Exception e) {
       e.printStrackTrace();
     }
  }
}

And this is how I call it:

private MyTestChildClass cmpTest = null;

// ...

// this.cmpTest == null
this.initTab(this.tabItemTest, this.cmpTest, MyTestChildClass.class);
// this.cmpTest == null <- Why is this still null?





Aucun commentaire:

Enregistrer un commentaire