views:

229

answers:

2

I'm experimenting with TextArea and inheritance to implement some additional functionality on the protected textField property.

Unfortunately, when I create a new instance of the subclass, this property is set to null. I'm probably misunderstanding the way super() works, but I thought it would have been instantiated after the constructor finished.

Here's a small snippet of code which extends TextArea:

public final class ExtTextArea extends TextArea {

  public function ExtTextArea() {
    super();
  }

  public function testTextField():void {
    if (textField == null)
      Alert.show("null!");
    }
  }
}

The invoking code is simple:

var extTextArea:ExtTextArea = new ExtTextArea();
extTextArea.testTextField();

The Alert in ExtTestArea appears every time I run this code.

Why is this? Is there something more I need to do to access the textField property?

+1  A: 

Since textField is "the internal UITextField that renders the text of this TextArea" I believe it will remain null until you add it to the display via .addChild(...). I ran a quick test to verify that once I've added it to the display, it is no longer null. You might want to add an event handler to the "creation complete" event and adjust it at that point (I think).

nevets1219
Thanks for the pointer. I've seen the same behavior, once the text area is added.
bedwyr
+1  A: 

The Flex SDK comes with source code, so you can take a peek and see when this field is initialized. It is not initialized in the constrcutor, but you will see that a new TextField instantiated by createChildren(), which is called when the component is added to a layout container.

Chris Thornhill