5 Replies Latest reply on Feb 24, 2009 1:33 AM by cpthk

    Flex running order?

    cpthk Level 1
      Here is my main code:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:comp="components.*" initialize="xmlData.send()">

      <mx:Script>
      <![CDATA[

      import mx.collections.ArrayCollection;
      import mx.controls.Alert;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      public var dataGridData:ArrayCollection;

      private function httpResultHandler(event:ResultEvent):void {
      dataGridData = event.result.account.data;
      }

      ]]>
      </mx:Script>
      <mx:HTTPService url="index2.php" id="xmlData" result="httpResultHandler(event)" showBusyCursor="true"/>

      <comp:totalPanel dataGridData="{dataGridData}" x="1060" y="412"/>
      </mx:Application>
      ********** End of main*********

      Here is my component file totalPanel:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Panel xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;

      public var dataGridData:ArrayCollection;
      private var dataLength:Number = dataGridData.length;

      ]]>
      </mx:Script>

      </mx:Panel>
      ********** End of component **********

      I run my code, it will give me a "Cannot access a property or method of a null object reference" error. I double checked, I realized when the program run until the component file up to the accessing dataGridData variable, that dataGridData variable is null at that time. That is probably flex initiate the component file before it send or get the xml data from server. Which cause the variable is still null at the mean time.

      Is there any solution to that? I actually got many similar problem while coding.
        • 1. Re: Flex running order?
          Karl_Sigiscar_1971 Level 3

          A better approach is to use MVC. You put your data into the model and it is accessible from anywhere (since the model is a singleton). In your case, it would be accessible by both main and your component and you wouldn't get any error.



          • 2. Flex running order?
            cpthk Level 1
            Can you give me a example of how to change my code to that?

            My previous code actually could get data too, but the problem is it get data after I need to access it. Flex seems to put in data after creation of component.
            • 3. Re: Flex running order?
              ntsiii Level 3
              First, only do simple assignments outside of a function. Specifically, do this:
              private var dataLength:Number = dataGridData.length;
              inside of a function called on creationComplete.

              Because of the way mxml is generated into an AS class, dataFridData does not exist when you are trying to use it
              • 4. Re: Flex running order?
                ntsiii Level 3
                Actually, declare the var in instance scope, as yo have, just do not try to access an objects in an assignment until creationComplete is called.
                • 5. Re: Flex running order?
                  cpthk Level 1
                  I tried to access it inside creationcomplete, but i realized sometimes still happen, but sometimes fine. I think the problem is flex initiate code too fast, that the server might still not respond yet. So which cause data not send into flex client side yet. So flex cannot access that. I think I need to create a dataGridData setter function so guarantee access after data sent into flex. Anyone know how do I create a setter function?