FLEX: the custom component is still a Null Object when I invoke its method

Posted by Patrick on Stack Overflow See other posts from Stack Overflow or by Patrick
Published on 2010-04-18T18:07:58Z Indexed on 2010/04/18 18:13 UTC
Read the original article Hit count: 351

Filed under:

Hi,

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

© Stack Overflow or respective owner

Related posts about flex