views:

59

answers:

1

This is really bugging me, but I have a component where a Button is wrapped in BorderContainer. I'm passing a custom property to the component at run-time to change the label of the button but Flex is reporting the following error:

Cannot access a property or method of a null object reference

When the error occurs, Flex highlights the following code:

myButton.label = value;

Here's the app:

// MyApp.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:local="*">
    <local:MyComp id="myButton" label="My Button"/>
</s:WindowedApplication>

// MyComp.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   width="400" height="300">
    <fx:Script>
        <![CDATA[
            private var _label:String;

            public function get label():String
            {
                return _label;
            }

            public function set label(value:String):void
            {
                _label = value;
                myButton.label = value;
            }
        ]]>
    </fx:Script>
    <s:Button id="myButton" label="Test"/>
</s:BorderContainer>

Any help would be greatly appreciated. Thanks in advance.

A: 

The myButton Object is not already created if the setter function for the label property is called the first time. Assign the new label value to myButton.label in commitProperties().

You should read About creating advanced components (most notably "About the component instantiation life cycle") to understand why.

splash
If I rename BorderContainer to Group and try again, it works. Any reason why BorderContainer is causing this error?
Reado
No idea. Maybe the `creationpolicy` is different.
splash