There is a timing problem.

The script is running before myComponent is actually created so you
get an error to the effect that you are trying to access a property of
a null object.

The best way is to wait for the object you are trying to access to be
created and then access it. One way is to use the creationComplete event:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" xmlns:local="*"
        creationComplete="onCreationComplete()">
        <mx:Script>
                <![CDATA[
                        public var myProperty:String;
                        
                        private function onCreationComplete():void
                        {
                                myProperty = myComponent.myFirstProperty;
                        }
                ]]>
        </mx:Script>
        <local:MyFirstComponent id="myComponent" myFirstProperty="Welcome to
Flex" />
        <mx:Label text="{myComponent.myFirstProperty}" id="myLabel"/>
</mx:Application>

--- In [email protected], "ashlytu" <[EMAIL PROTECTED]> wrote:
>
> when I read "the Essential guide to flex 3"
> from page 87 to 95, there is a example , in the application MXML ,the
> code is
> 
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
> layout="absolute" xmlns:local="*">
>     <mx:Script>
>       <![CDATA[
>           
>               public var myProperty:String = myComponent.myFirstProperty;
>       
>       ]]>
>     </mx:Script>
>       <local:MyFirstComponent  id="myComponent" myFirstProperty="Welcome to
> Flex" />
>     <mx:Label text="{myComponent.myFirstProperty}" id="myLabel"/>
>     
> </mx:Application>
> 
> and in the component file :MyFirstComponent ,is just these code below:
> 
> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"; width="400"
> height="300">
>       <mx:Script>
>               <![CDATA[
>                       [Bindable]
>                       public var myFirstProperty:String;
>               ]]>
>       </mx:Script>
> </mx:Canvas>
> 
> I followed all the steps, and would like to view the value of
> "myProperty" in the script section of the Application MXML file, in
> the Expressionis frame, but the error was "TypeError: Error #1009:",
> It's seems like "myProperty" cannot get the value from a object
attribute.
> 
> what is wrong with it?
> Thanks a lot!
> 
> Ashly
>


Reply via email to