title: Parsley dependency injection problem with Flex 4 using Presentation Model pattern
body: <p>I have a View class EmployeeList as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009" 
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:parsley="http://www.spicefactory.org/parsley"
                      xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"&gt;
  
  
      &lt;fx:Script&gt;
          &lt;![CDATA[
              import cafeparsley.model.EmployeeListPM;
  
              [Inject]
              [Bindable]
              public var model : EmployeeListPM;
  
              [Init]
              public function init () : void {
                  model.init();
              }
  
  
          ]]&gt;
      &lt;/fx:Script&gt;    
  
      &lt;s:Panel title="Employee List" horizontalCenter="0"&gt;
          &lt;s:HGroup paddingTop="50"&gt;
              &lt;s:Button label="Add New Employee" click="model.addNewEmployee()" /&gt;
              &lt;mx:Spacer width="100%" /&gt;
              &lt;s:Button label="Logout" click="model.logout()" /&gt;
              &lt;mx:Spacer width="100%" height="20" /&gt;  
          &lt;/s:HGroup&gt;
          &lt;s:List id="empList" dataProvider="{ model.employees }" labelFunction="model.properName" 
                   change="model.initUpdateEmployee(empList.selectedItem);empList.selectedIndex = -1;" width="100%" /&gt;
          &lt;s:Label id="error" color="0xFF0000" /&gt;
      &lt;/s:Panel&gt;  
  
  &lt;/s:NavigatorContent&gt;
  </code></pre>
  
  <p>The PM looks like this:</p>
  
  <pre><code>package cafeparsley.model
  {
      import cafeparsley.events.EmployeeEvent;
      import cafeparsley.events.NavigationEvent;
      import cafeparsley.services.impl.EmployeeServiceImpl;
      import cafeparsley.vo.Employee;
  
      import flash.events.EventDispatcher;
  
      import mx.collections.ArrayCollection;
      import mx.rpc.IResponder;
  
      [Bindable]
      [Event(name="navigationEvent", type="cafeparsley.events.NavigationEvent")]
      [ManagedEvents("navigationEvent")]
      public class EmployeeListPM extends EventDispatcher implements IResponder
      {
  
          public var employeeService : EmployeeServiceImpl = new EmployeeServiceImpl();
  
          public var employees : ArrayCollection;
  
          public function init() : void
          {
              loadEmployees();
          }
  
  
          public function EmployeeListPM() 
          {
          }
  
          public function loadEmployees():void
          {
              employeeService.loadEmployees( this );
          }  
  </code></pre>
  
  <p>Regardless of whether I use  or autowiring to perform injection, when I run this I get a the following error message:</p>
  
  <p>TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at cafeparsley.view::EmployeeList/_EmployeeList_List1_i()[C:\dev\code\workspace\Examples\CafeParsley\src\cafeparsley\view\EmployeeList.mxml:29]
      at cafeparsley.view::EmployeeList/_EmployeeList_Array2_c()
      at mx.core::DeferredInstanceFromFunction/getInstance()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\DeferredInstanceFromFunction.as:105]
      at spark.components::SkinnableContainer/createDeferredContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:985]
      at spark.components::SkinnableContainer/createContentIfNeeded()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:1014]
      at spark.components::SkinnableContainer/createChildren()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:827]
      at mx.core::UIComponent/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7349]
      at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7241]
      at mx.core::UIComponent/addChildAt()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:6947]
      at spark.components::Group/addDisplayObjectToDisplayList()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:1825]
      at spark.components::Group/http://www.adobe.com/2006/flex/mx/internal::elementAdded()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:1416]
      at spark.components::Group/setMXMLContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:512]
      at spark.components::Group/set mxmlContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\Group.as:452]
      at spark.components::SkinnableContainer/set mxmlContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:604]
      at spark.components::SkinnableContainer/createDeferredContent()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:986]
      at spark.components::SkinnableContainer/createContentIfNeeded()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:1014]
      at spark.components::SkinnableContainer/createChildren()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\SkinnableContainer.as:827]
      at spark.components::NavigatorContent/createChildren()[E:\dev\4.x\frameworks\projects\spark\src\spark\components\NavigatorContent.as:225]
      at mx.core::UIComponent/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7349]
      at cafeparsley.view::EmployeeList/initialize()</p>
  
  <p>So the employeeListPM is null when the error is thrown. However if I comment out the
  <code>&lt;s:List&gt;</code> component, rerun and set a breakpoint in the init method, init() will get called. So it's not that my context config is wrong, it's just that the PM hasn't been injected in time and the error is thrown. But according to the Parsley manual if I use autowiring or <code>&lt;parsley:configure/&gt;</code> the PM should be injected by the time it is needed. </p>
  
  <p>I can't see what is I am doing wrong in what I thought was a relatively trivial dependency injection scenario. Can you help?</p>