title: Selection in Flex Datagrid does not pass the valueObject to selectionChangeHandler function
body: <p>I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.</p>
  
  <p>There are several problems:
  1) Forms are not populated with data from the datagrid selection.
  2) Selecting a row and clicking delete gives the very-common error: <code>TypeError: Error #1009: Cannot access a property or method of a null object reference.</code> 
  A trace on the valueObject unit within the selectionChangeHandler function gives NULL. Why?</p>
  
  <p>Note: In other modules (other tabs of the TabNavigator), I have DropDownLists populated with units. This means that the valueObject Unit is defined in the other modules. However, valueObjects should be private to modules, and not shared. I am unsure where the problem comes.</p>
  
  <p>Full module code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx"
            xmlns:unitservice="services.unitservice.*"
            xmlns:valueObjects="valueObjects.*"
            width="724"
            height="674"&gt;
      &lt;fx:Style source="assets/CAaNDFlex.css"/&gt;
      &lt;fx:Script&gt;
          &lt;![CDATA[
              import mx.controls.Alert;
              import mx.events.FlexEvent;
              import mx.rpc.events.ResultEvent;
  
              import spark.events.GridSelectionEvent;
  
              protected function unitsDg_creationCompleteHandler(event:FlexEvent):void
              {
                  getUnitsResult.token=unitservice.getUnits();
              }
  
              protected function addBtn_clickHandler(event:MouseEvent):void
              {
                  currentState="unitsAdd";
                  unit=new Unit();
              }
  
              protected function unitsDg_selectionChangeHandler(event:GridSelectionEvent):void
              {
                  trace(event.currentTarget.selectedItem); //Unit object detected
                  trace(event.currentTarget.selectedItem as Unit); //NULL 
                  trace(unit); // unit is NULL. Why?
                  currentState="unitsDetails";
              }
  
  
              protected function button_clickHandler(event:MouseEvent):void
              {
                  trace(unit); // unit is NULL. Why?
                  unit.unitName=unitNameTextInput.text;
                  if (unit.unitID == 0)
                  {
                      createUnitResult.token=unitservice.createUnit(unit);
                  }
                  else
                  {
                      updateUnitResult.token=unitservice.updateUnit(unit);
                  }
              }
  
              protected function updateBtn_clickHandler(event:MouseEvent):void
              {
                  currentState="unitsUpdate";
              }
  
              protected function createUnitResult_resultHandler(event:ResultEvent):void
              {
                  currentState="unitsDetails";
                  unit.unitID=event.result as int;
                  unitsDg.dataProvider.addItem(unit);
                  unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(unit));
                  unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
              }
  
              protected function deleteBtn_clickHandler(event:MouseEvent):void
              {
                  deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);
  
              }
  
              protected function deleteUnitResult_resultHandler(event:ResultEvent):void
              {
                  unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
                  currentState="units";
              }
  
          ]]&gt;
      &lt;/fx:Script&gt;
      &lt;s:states&gt;
          &lt;s:State name="units"/&gt;
          &lt;s:State name="unitsDetails"/&gt;
          &lt;s:State name="unitsAdd"/&gt;
          &lt;s:State name="unitsUpdate"/&gt;
      &lt;/s:states&gt;
      &lt;fx:Declarations&gt;
          &lt;s:CallResponder id="getUnitsResult"
                           result="unit = getUnitsResult.lastResult as Unit"/&gt;
          &lt;unitservice:UnitService id="unitservice"
                                             fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                             showBusyCursor="true"/&gt;
          &lt;valueObjects:Unit id="unit" /&gt;
          &lt;s:CallResponder id="createUnitResult"
                           result="createUnitResult_resultHandler(event)"/&gt;
          &lt;s:CallResponder id="updateUnitResult"/&gt;
          &lt;s:CallResponder id="deleteUnitResult" result="deleteUnitResult_resultHandler(event)"/&gt;
  
  
  
          &lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt;
      &lt;/fx:Declarations&gt;
      &lt;fx:Binding destination="unit" source="unitsDg.selectedItem as Unit"/&gt;
  
      &lt;s:DataGrid id="unitsDg" x="10" y="37"
                  creationComplete="unitsDg_creationCompleteHandler(event)" requestedRowCount="4"
                  selectionChange="unitsDg_selectionChangeHandler(event)"&gt;
          &lt;s:columns&gt;
              &lt;s:ArrayList&gt;
                  &lt;s:GridColumn dataField="unitName"
                                headerText="unitName"&gt;
                  &lt;/s:GridColumn&gt;
                  &lt;s:GridColumn dataField="unitID"
                                headerText="unitID"&gt;
                  &lt;/s:GridColumn&gt;
              &lt;/s:ArrayList&gt;
          &lt;/s:columns&gt;
          &lt;s:typicalItem&gt;
              &lt;fx:Object unitID="unitID1"
                         unitName="unitName1"&gt;
              &lt;/fx:Object&gt;
          &lt;/s:typicalItem&gt;
          &lt;s:AsyncListView list="{getUnitsResult.lastResult}"/&gt;
      &lt;/s:DataGrid&gt;
      &lt;s:Button id="addBtn" x="10" y="0" label="Add" click="addBtn_clickHandler(event)"
                styleName="actionButton"/&gt;
      &lt;s:Form includeIn="unitsAdd,unitsUpdate"
              x="10"
              y="176"
              defaultButton="{button}"&gt;
          &lt;s:FormItem label="unitName"&gt;
              &lt;s:TextInput id="unitNameTextInput"
                           text="{unit.unitName}"/&gt;
          &lt;/s:FormItem&gt;
          &lt;s:Button id="button"
                    label="Add"
                    click="button_clickHandler(event)"
                    label.unitsUpdate="Update"/&gt;
      &lt;/s:Form&gt;
      &lt;s:Button id="updateBtn" x="138" y="0" label="Update" click="updateBtn_clickHandler(event)"/&gt;
      &lt;s:Button id="deleteBtn" x="266" y="0" label="Delete" click="deleteBtn_clickHandler(event)"/&gt;
      &lt;s:Form includeIn="unitsDetails" x="10" y="176"&gt;
          &lt;s:FormItem label="unitName"&gt;
              &lt;s:Label id="unitNameLabel" text="{unit.unitName}"/&gt;
          &lt;/s:FormItem&gt;
      &lt;/s:Form&gt;
  &lt;/s:Module&gt;
  </code></pre>