3 Replies Latest reply on Sep 28, 2009 11:51 AM by dimiorla

    addEventListener and TabNavigator error 1009

    dimiorla

      Hey everyone,

      The code below works, but when I transfer the datagrid and text to the second tad of the TabNavigator I get error

      Error #1009: Cannot access a property or method of a null object reference.
      Is thear a work around for this problem?


      Thanks in advance,
      Dimitris Orlandos.

       

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

      <mx:Application

      creationComplete="loadData()" styleName="plain" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

      <mx:Script>

      <![CDATA[

       

      import mx.controls.dataGridClasses.DataGridItemRenderer;

       

      import mx.events.DragEvent;

       

      import mx.events.DataGridEvent;

       

      import mx.events.FlexEvent;

       

      import mx.controls.listClasses.ListItemRenderer;

       

      import mx.collections.ArrayCollection;

       

      import mx.events.CollectionEvent;

       

      private

       

      function initApp():void {

      OrdersOpenDataGrid.dataProvider =

      new ArrayCollection([

      {Type:

      'Normal', Desc:'Normal 20/20', Cost:0},

      {Type:

      'None', Desc:'Player has no optical receptors', Cost:-2},

      {Type:

      'Enhanced', Desc:'Enhanced Vision', Cost:5},

      {Type:

      'Infrared', Desc:'Player can see heat sources', Cost:5}

      ]);

      }

      private

       

      function loadData():

      void

      {

      OrdersOpenDataGrid.addEventListener(CollectionEvent.COLLECTION_CHANGE,calculateTotalColumn );

      initApp();

      }

      private

       

      function calculateTotalColumn(evt:CollectionEvent):void

      {

       

      var totalColumn:Number = 0;

       

      for each (var row:Object in OrdersOpenDataGrid.dataProvider)

      {

      totalColumn += Number(row.Cost);

      }

      text1.text = totalColumn.toFixed(2);

      }

      ]]>

       

      </mx:Script>

       

      <mx:TabNavigator x="21" y="10" width="442" height="329">

       

      <mx:Canvas label="Tab 1" width="100%" height="100%">

       

      <mx:DataGrid id="OrdersOpenDataGrid" x="51" y="11">

       

      <mx:columns>

       

      <mx:DataGridColumn dataField="Type"/>

       

      <mx:DataGridColumn dataField="Desc"/>

       

      <mx:DataGridColumn dataField="Cost"/>

       

      </mx:columns>

       

      </mx:DataGrid>

       

      <mx:Text x="139" y="197" height="33" fontWeight="bold" fontSize="16" id="text1" width="111"/>

       

      </mx:Canvas>

       

      <mx:Canvas label="Tab2" width="100%" height="100%">

       

      </mx:Canvas>

       

      </mx:TabNavigator>

      </mx:Application>

        • 1. Re: addEventListener and TabNavigator error 1009
          drillnaut Level 1

          It has to do with creation policy.  When you are adding the listener the grid isn't actually created , therefore it is null.  The code below works.  Notice how I  loadData runs when the datagrid fires the creationComplete event.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application styleName="plain" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>

          <![CDATA[
          import mx.controls.dataGridClasses.DataGridItemRenderer;
          import mx.events.DragEvent;
          import mx.events.DataGridEvent;
          import mx.events.FlexEvent;
          import mx.controls.listClasses.ListItemRenderer;
          import mx.collections.ArrayCollection;
          import mx.events.CollectionEvent;

          private function initApp():void
          {
          OrdersOpenDataGrid.dataProvider =
          new ArrayCollection([
          {Type:'Normal', Desc:'Normal 20/20', Cost:0},
          {Type:'None', Desc:'Player has no optical receptors', Cost:-2},
          {Type:'Enhanced', Desc:'Enhanced Vision', Cost:5},
          {Type:'Infrared', Desc:'Player can see heat sources', Cost:5}
          ]);
          }

          private function loadData():void
          {
          OrdersOpenDataGrid.addEventListener(CollectionEvent.COLLECTION_CHANGE,calculateTotalColumn );
          initApp();
          }

          private function calculateTotalColumn(evt:CollectionEvent):void
          {
          var totalColumn:Number = 0;
          for each (var row:Object in OrdersOpenDataGrid.dataProvider)
          {totalColumn += Number(row.Cost);}
          text1.text = totalColumn.toFixed(2);
          }
          ]]>

          </mx:Script>
          <mx:TabNavigator x="21" y="10" width="442" height="329">
          <mx:Canvas label="Tab2" width="100%" height="100%">
          </mx:Canvas>
          <mx:Canvas label="Tab 1" width="100%" height="100%">
          <mx:DataGrid id="OrdersOpenDataGrid" x="51" y="11" creationComplete="{loadData()}">
          <mx:columns>
          <mx:DataGridColumn dataField="Type"/>
          <mx:DataGridColumn dataField="Desc"/>
          <mx:DataGridColumn dataField="Cost"/>
          </mx:columns>
          </mx:DataGrid>
          <mx:Text x="139" y="197" height="33" fontWeight="bold" fontSize="16" id="text1" width="111"/>
          </mx:Canvas>
          </mx:TabNavigator>
          </mx:Application>

          • 2. Re: addEventListener and TabNavigator error 1009
            paul.williams Level 4

            By default the viewstack will defer the full creation of child views until they are accessed by the user. This is for performance reasons, but you can alter this behaviour by modifying the creationPolicy property. Read more here:

             

            http://livedocs.adobe.com/flex/3/html/layoutperformance_04.html

            http://livedocs.adobe.com/flex/3/html/layoutperformance_05.html

             

            Switching to a creationPolicy of "All" will probably prevent the error you are seeing, but is not recommended as it will reduce performance as your application scales up.

             

            An alternative solution would be to convert your child Canvas into a custom MXML component, move the logic from your main application into this component, and trigger loadData from this component's creationComplete event handler.

            1 person found this helpful
            • 3. Re: addEventListener and TabNavigator error 1009
              dimiorla Level 1

              Thanks a lot guys.

              It is nice to know that there someone out there watching out for us newbs