1 Reply Latest reply on Aug 26, 2006 9:47 AM by Renaun_Erickson

    Recieving Error #1009 on attempts to expandItem on Tree object

    Level 1
      Hello Forum Folks,

      I'm recieving a "Error #1009:Cannot acces a property or method of a null object reference" when attempting to run expandItem on a Tree object. I have a Tree class via mxml that is using XMLList as a data provider:

      <mx:Tree xmlns:mx=" http://www.adobe.com/2006/mxml"
      height="100%"
      width="100%"
      labelField="@label"
      dataDescriptor="{new DefaultDataDescriptor()}">

      <mx:XML id="authenticatedXML" source="pmportal_menu_authenticated.xml"/>
      <mx:XML id="notauthenticatedXML" source="pmportal_menu_notauthenticated.xml"/>

      <mx:Script>
      <![CDATA[
      import mx.controls.treeClasses.DefaultDataDescriptor;
      import mx.collections.XMLListCollection;

      public function setAuth():void
      {
      this.dataProvider = authenticatedXML;
      var xmlListCollection:XMLListCollection = XMLListCollection(this.dataProvider);
      this.expandItem(xmlListCollection.getItemAt(0), true, true, true);
      // var xmlList:XMLList = xmlListCollection.children();
      // this.expandItem(xmlList, true, true, true);
      // var xmlList2:XMLList = xmlList.child(3);
      // this.selectedItem = xmlList;
      }

      ]]>
      </mx:Script>
      </mx:Tree>

      Any attempts to expand any of the nodes of the tree end in the error. Upon debugging this in Flex Builder2, I notice that it was failing on this method in the Tree.as class:

      private function isBranch(item:Object):Boolean
      {
      if (item != null)
      return _dataDescriptor.isBranch(item, iterator.view);

      return false;
      }

      Upon looking at the variables of the Tree object, I notice that the iterator was null. Now, the iterator doesn't seem to be a class variable as it is not declared in the Tree class (maybe declared in the one of the subclasses?), so I am not sure when this variable is to contain a value.

      OK... I just found the answer... and in hopes of keeping someone like me from banging their heads on the wall for fours here is the answer.

      It appears that the iterator variable will have a value with the creation of the Tree is complete. So, I just called the expandItem method after the Tree is completed, and everything works. Here's the changed code:

      <mx:Tree xmlns:mx=" http://www.adobe.com/2006/mxml"
      height="100%"
      width="100%"
      labelField="@label"
      dataDescriptor="{new DefaultDataDescriptor()}"
      creationComplete="creationComplete()">

      <mx:XML id="authenticatedXML" source="pmportal_menu_authenticated.xml"/>
      <mx:XML id="notauthenticatedXML" source="pmportal_menu_notauthenticated.xml"/>

      <mx:Script>
      <![CDATA[
      import mx.controls.treeClasses.DefaultDataDescriptor;
      import mx.collections.XMLListCollection;

      public function setAuth():void
      {
      this.dataProvider = authenticatedXML;
      }

      public function setNotAuth():void
      {
      this.dataProvider = notauthenticatedXML;
      }

      public function creationComplete():void
      {
      var xmlListCollection:XMLListCollection = XMLListCollection(this.dataProvider);
      this.expandItem(xmlListCollection.getItemAt(3), true, true, true);
      }

      ]]>
      </mx:Script>
      </mx:Tree>