2 Replies Latest reply on May 18, 2010 6:40 AM by released87

    ViewStack/Null Object Reference

    released87 Level 1

      Hi,

       

      I am struggling to develop a solution for the navigation system in my Flex App. I have a ViewStack within my main application file. Each child within that ViewStack has it's own ViewStack, and those children are the actual pages.

       

      I am using a Menu Bar to change both the root ViewStack and the child ViewStack:

       

                  private function select(event:MenuEvent):void {
                      if(event.item.@level == "1"){
                          _ROOT.View.selectedIndex= event.item.@index as uint;
                      }
                      if(event.item.@level == "2"){
                          _ROOT.View.selectedChild= _ROOT[event.item.@parent];
                          _ROOT[event.item.@parent].View.selectedIndex= event.item.@index;
                      }
                      if(event.item.@level == "3"){
                          _ROOT.View.selectedChild= _ROOT[event.item.@parent];
                          _ROOT[event.item.@parent].View.selectedIndex= event.item.@index;
                          _ROOT[event.item.@parent].View[event.item.@index].selectedIndex= event.item.@index;
                      }
                  }

       

      Menu Items are nested 3 levels deep. I am still on the second level. I get caught up on the highlighted line, it throws me a Null Object Reference because the ViewStack Child has not been instantiated yet and so therefore cannot be loaded through the menu bar.

       

      How can I instantiate each child as needed via actionscript? If I could add a line prior to the purple line to instantiate the component then it should not be null when it is referenced in the 'selectedIndex' property I think that would do the job.

       

      Much thanks,

      Jesse

        • 1. Re: ViewStack/Null Object Reference
          UbuntuPenguin Level 4

          Well , you could tell all the viewstacks to have a creation policy set to "all".  This would ensure that the children are all created thus avoiding the null pointer exception.  The better idea is to use a framework , or a bindable variable which would tell the viewstack which index it should be on.  For example I use Mate , so I would inject the stackIndex variable from a presentation class.

           

          <ViewStack ... selectedIndex="{stackIndex}">

          ...

          [Bindable] public var stackIndex:int;

           

          </ViewStack>

           

          This way is better because it allows deferred instantiation , cutting down startup time , and shows seperation of concern

          • 2. Re: ViewStack/Null Object Reference
            released87 Level 1

            Thank you very much!!

             

            I ended up setting a property on the main application file 'Master'. I set the index of the primary viewstack with this bindable variable. I set the creation policy of this primary viewstack to 'none' and then set the index dynamically.

             

            I used creationPolicy 'all' for each sub-stack. This way, only the first child of the main application viewstack loads, and then when the other loads, it then loads each of the children.


            Thanks again!