7 Replies Latest reply on Apr 27, 2010 5:21 AM by SiHoop

    Why does a trace statement crash the file?

    SiHoop Level 1

      The following file uses a component: CustomButton

      CustomButton has a Text field with an id of buttonLabel. and a text value ="My Button"

       

      When I trace button1.buttonLabel.text  I get the following error message:

       

      Cannot access a property or method of a null object reference

      What have I done wrong?

       

      Thank you!

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  applicationComplete="init()"  >
      <mx:Script>
        <![CDATA[
      private var button1:CustomButton;
      public function init():void{
          button1=new CustomButton;
          trace("Text="+button1.buttonLabel.text)
          this.addChild(button1)
      }
        ]]>
      </mx:Script>
      </mx:Application>

       

      CustomButton.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
          <mx:Text  id="buttonLabel" text="My Button"/>
      </mx:Canvas>

        • 1. Re: Why does a trace statement crash the file?
          smagfx2 Level 1

          I'm guessing it is because your CustomButton component is not fully created (even though your application is). In Flex, doing new SomeComponent() does not fully create the component and most of its properties are null / not filled in at that point. You need to at least wait until after addChild() to access children of the component.

           

          Instead of creating CustomButton in the init() function, why not use this in the MXML part of the main app:

           

          <local:CustomButton id="button1" />
          

          1 person found this helpful
          • 2. Re: Why does a trace statement crash the file?
            David_F57 Level 5

            hi,

             

            try   button1 = new CustomButton()   ;

             

             

             

            David.

            • 3. Re: Why does a trace statement crash the file?
              SiHoop Level 1

              Your answer is correct. However, if I stay with AS3, which listener would I use to make sure the component fully exists before I use it? I tried

               

              button2.addEventListener(Event.COMPLETE, handler)

               

              but this doesn't work.

               

               

              <?xml version="1.0" encoding="utf-8"?>

              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" layout="absolute"  creationComplete="init()"  >

              <mx:Script>

                <![CDATA[

              private var button2:CustomButton;

              public function init():void{

                  button2=new CustomButton();

                  trace("button1.text="+button1.buttonLabel.text)

                  button2.addEventListener(Event.COMPLETE, handler)

                  this.addChild(button1)

              }

              public function handler(e:Event):void{

                  trace("button2.text="+button2.buttonLabel.text)

              }

                ]]>

              </mx:Script>

              <local:CustomButton id="button1" />

              </mx:Application>

              • 4. Re: Why does a trace statement crash the file?
                David_F57 Level 5

                hi,

                 

                On the apps creationcomplete you can create the button then add the eventlistener

                 

                 

                button1 = new custbutton();

                button1.addEventListener(mouseEvent.CLICK,button1_Handler);

                 

                you shouldn't need more than that.

                 

                David.

                • 5. Re: Why does a trace statement crash the file?
                  SiHoop Level 1

                  David,

                  Thanks for your reply, but that doesn't solve the original problem. I want to access the property of the button as soon as the button is added to the display. Your solution waits for the user to click the button before returning a value. Can I use a listener to wait for the button to be added to the display before triggering an event? If so, which one should I use?

                  • 6. Re: Why does a trace statement crash the file?
                    David_F57 Level 5

                    hi,

                     

                    I was under the impression that you had trouble with event listeners. Once the button is created you don't need to add it to the stage to get at the properties.

                     

                     

                    var myButton:CustButton = new CustButton();

                     

                     

                    init....

                     

                    myButton.x=10;

                    myButton.y=10;

                    myButton.label ="helloworld";

                    myButton.newproperty="hello all"

                     

                    this.addChild(myButton).

                     

                    If you can't see properties after the button is created then the component itself is initialising incorrectly

                     

                     

                    David.

                    • 7. Re: Why does a trace statement crash the file?
                      SiHoop Level 1

                      I think that smagfx2 was correct. in saying that the component had not loaded. I worked around the problem by dispatching an event as follows. I wish I could have used an existing listener:

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" layout="absolute"  creationComplete="init()"  >
                      <mx:Script>
                        <![CDATA[
                      private var button2:CustomButton;
                      public function init():void{
                          button2=new CustomButton();
                          trace("button1.text="+button1.buttonLabel.text)
                          button2.addEventListener(CustomButton.DATA_LOADED, handler);
                          this.addChild(button2)
                         
                      }
                      public function handler(e:Event):void{
                          trace("button2.text="+button2.buttonLabel.text)
                          button2.buttonLabel.text="success"
                          button2.x+=300
                      }
                        ]]>
                      </mx:Script>
                      <local:CustomButton id="button1" />

                       

                      </mx:Application>

                       

                       

                      CustomButton.mxml:

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()" >
                      <mx:Script>
                          <![CDATA[
                              public static const DATA_LOADED:String="dataLoaded";
                              public function init():void{
                                  dispatchEvent(new Event(CustomButton.DATA_LOADED));
                              }
                          ]]>
                      </mx:Script>
                          <mx:Text  id="buttonLabel"  width="111" text="My Button"/>
                      </mx:Canvas>