2 Replies Latest reply on Aug 14, 2008 9:36 AM by Newsgroup_User

    Setting Listener on DataGrid in a ViewStack

    CenturyMan1979 Level 2
      Ok so I have a viewstack in which in the 3rd view I have set up a datagrid. Now at the start of the application I am trying to set a double click listener up on the datagrid like so,

      contactList.doubleClickEnabled = true;
      contactList.addEventListener(ListEvent.ITEM_CLICK, doubleClickHandler);

      Now this code will work on a datagrid that is in the first view but when the datagrid is in any of the other views I get this error from flash,

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at index/initApp()[C:\_Work\Renaissance Event Planner\src\index.mxml:38]
      at index/___index_Application1_creationComplete()[C:\_Work\Renaissance Event Planner\src\index.mxml:3]
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at mx.core::UIComponent/dispatchEvent()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\co re\UIComponent.as:9051]
      at mx.core::UIComponent/set initialized()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\UIComponent.as:1167]
      at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\3.0.x\frameworks\projects\frame work\src\mx\managers\LayoutManager.as:698]
      at Function/ http://adobe.com/AS3/2006/builtin::apply()
      at mx.core::UIComponent/callLaterDispatcher2()[E:\dev\3.0.x\frameworks\projects\framework\sr c\mx\core\UIComponent.as:8460]
      at mx.core::UIComponent/callLaterDispatcher()[E:\dev\3.0.x\frameworks\projects\framework\src \mx\core\UIComponent.as:8403]

      So what am I doing wrong and how do I get the listener associated with the datagrid in a view that is not being displayed?
        • 1. Re: Setting Listener on DataGrid in a ViewStack
          Gregory Lafrance Level 6
          Easiest way is to set the ViewStack creationPolicy="all", but this can affect performance depending on what is in the ViewStack. Another way is to ensure whatever will be referenced is initialized, perhaps in the ViewStack creationComplete or some other event. Then creationPolicy can be the default of "auto".
          • 2. Re: Setting Listener on DataGrid in a ViewStack
            Level 7

            "CenturyMan1979" <webforumsuser@macromedia.com> wrote in message
            news:g81iuk$hta$1@forums.macromedia.com...
            > Ok so I have a viewstack in which in the 3rd view I have set up a
            > datagrid. Now
            > at the start of the application I am trying to set a double click listener
            > up
            > on the datagrid like so,
            >
            > contactList.doubleClickEnabled = true;
            > contactList.addEventListener(ListEvent.ITEM_CLICK, doubleClickHandler);
            >
            > Now this code will work on a datagrid that is in the first view but when
            > the
            > datagrid is in any of the other views I get this error from flash,
            >
            > TypeError: Error #1009: Cannot access a property or method of a null
            > object
            > reference.

            By default, the Navigator containers only create the children of each pane
            as that pane is viewed. The easy way to fix this is to set the
            creationPolicy on the Navigator to "all." However, this will cause your
            application to take longer to load. A better way to fix this is to wait for
            a later event, such as creationComplete on the component you want to access,
            or to use binding to "pull" the data into the component. The way I handle
            it is to call invalidateProperties() on change of the ViewStack. I then
            override commitProperties() and call an "initializer" for each pane. In the
            body of each initializer function, I check to see if the selectedItem for
            the viewStack is the one my initalizer cares about. If not, I return from
            the function immediately. Inside that initializer function, I set
            properties and add listeners as appropriate.