2 Replies Latest reply on Feb 10, 2007 12:14 PM by MrBritton

    Nested Component Initialization Blowup

    MrBritton
      Sorry if this is covered somewhere - it seems so basic that I have to assume it is, but it's not obvious to me...

      I have a component B inside a component A.
      Component A has a set function, set foo(...) that ends up manipulating component B.
      If component A is instantiated as: <local:A foo="bar" />, there is a runtime failure indicating that component B is null.

      I can see ways to code around this, but can this really be the case - that at initialization time for A, B is not ready?

      Thanks,

      Just in case I wasn't clear, here's some test code:
      Component B:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:TextInput xmlns:mx=" http://www.adobe.com/2006/mxml" />

      Component A:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:local="*" >
      <mx:Script><![CDATA[
      public function set txt( s:String ) : void {
      mybox.text = s;
      }
      ]]></mx:Script>

      <local:TestBox id="mybox" />

      </mx:Canvas>

      Application Code:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:local="*" >
      <local:TestBoxBox txt="foo" />
      </mx:Application>
        • 1. Re: Nested Component Initialization Blowup
          imad.m
          That's right your were trying to manipulate B before it was created, try using creationComplete event of the component B and call your setText function in it.

          by the way, your sample code isn't clear either , you didn't mention when you call you setText function.

          • 2. Re: Nested Component Initialization Blowup
            MrBritton Level 1
            quote:

            Originally posted by: imad.m
            That's right your were trying to manipulate B before it was created, try using creationComplete event of the component B and call your setText function in it.

            by the way, your sample code isn't clear either , you didn't mention when you call you setText function.



            The txt setter function gets called as a result of this line: <local:TestBoxBox txt="foo" /> (I didn't point out that Component B is called TextBox and Component A is called TextBoxBox).

            And that usage shows why using the creationComplete event is not at all convenient - I'd have to have some intermediate storage for txt, and then transfer it on creationComplete, and have the set txt() function check for Component B being null so that it would work normally when things are up and running - very ugly.

            My current solution is to create an intermediate variable in Component A, and then bind the appropriate attribute (text) of Component B to that.
            It was not obvious to me that Component B isn't ready when Component A is created, and that such extra measures need be taken to deal with that fact. I still hope there's some other approach.