I’ve created a custom component in Flex, and I’ve created it from the main application with actionscript. Successively I invoke its “setName” method to pass a String.
I get the following run-time error (occurring only if I use the setName method):
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I guess I get it because I’m calling to newUser.setName method from main application before the component is completely created.
How can I ask actionscript to “wait” until when the component is created to call the method ? Should I create an event listener in the main application waiting for it ? I would prefer to avoid it if possible.
Here is the code:
Main app
...
newUser = new userComp();
//newUser.setName("name");
Component:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100" height="200" >
<mx:Script>
<![CDATA[
public function setName(name:String):void {
username.text = name;
}
public function setTags(Tags:String):void {
}
]]>
</mx:Script>
<mx:HBox id="tagsPopup" visible="false">
<mx:LinkButton label="Tag1" />
<mx:LinkButton label="Tag2" />
<mx:LinkButton label="Tag3" />
</mx:HBox>
<mx:Image source="@Embed(source='../icons/userIcon.png')"/>
<mx:Label id="username" text="Nickname" visible="false"/>
</mx:VBox>
thanks
You get the error message because the Label component with id “username” has not been initialized when you call setName function.
you can create a property in UserComp, and set Label’s text property binded to it. And in your setName function, you assign the value to the property just created. When the Label component is created, it will use the value from the property to show on the screen.