开发者

Null Object Reference after creating the Object

开发者 https://www.devze.com 2023-03-29 23:48 出处:网络
I have one MXML File as <objecthandles:ObjectHandles xmlns:fx=\"http://ns.adobe.com/mxml/2009\"xmlns:s=\"libra开发者_StackOverflowry://ns.adobe.com/flex/spark\"backgroundAlpha=\"1\"xmlns:mx=\"lib

I have one MXML File as

<objecthandles:ObjectHandles xmlns:fx="http://ns.adobe.com/mxml/2009"                            xmlns:s="libra开发者_StackOverflowry://ns.adobe.com/flex/spark"  backgroundAlpha="1"                            xmlns:mx="library://ns.adobe.com/flex/mx"
allowRotate="true" minHeight="25" minWidth="60" height="100" width="200">

<s:BorderContainer  id="borderCon" width="100%" height="100%" minHeight="25" minWidth="60" 
                        backgroundAlpha="0" borderVisible="false" borderAlpha="0">
         <s:HGroup id="hgText" width="100%" height="100%" gap="0" >
            <mx:TextArea id="txtAdd" width="100%" height="100%" color="black" 
                         minHeight="25" minWidth="60" horizontalScrollPolicy="off" verticalScrollPolicy="off" focusOut="txtAddKeyUpHandler(event)"  
                         keyUp="txtAddMirrorKeyUpHandler(event)"
                         creationComplete="onTextCreationComplete()" />
        </s:HGroup>
</s:BorderContainer>

</objecthandles:ObjectHandles>

When ever i create the object of the Fileas

var txtElement:TextElement = new TextElement(); txtElement.txtAdd.text = "Hello";

Then it showing the null object reference that txtElement.txtAdd seems to be null

Need Perfect Solution?


In the Flex component lifecycle subcomponents will not be created until the parent component is added to the displaylist. Only when the component is added to the displaylist and fully built, will you be able to access its subcomponents. When the component is completely ready for usage, it will dispatch a FlexEvent.CREATION_COMPLETE event.

So do something like this:

var txtElement:TextElement = new TextElement();
txtElement.addEventListener(FlexEvent.CREATION_COMPLETE, initTxtElement);
addElement(txtElement);

private function initTxtElement(event:FlexEvent):void {
    txtElement.txtAdd.text = "Hello";
}

Or better yet, since it's a custom component: expose the 'text' property as a property of 'TextElement' and handle the deferred setting of the property internally, so that you can write: txtElement.text = "hello".

0

精彩评论

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