2 Replies Latest reply on Nov 2, 2010 7:01 AM by Devtron

    Why - AddChild using Panel(s) is NULL everytime except the first!

    Devtron Level 3



      I am having a problem with adding panels to a coverflow control, dynamically. Everything works great the first time around. But if I try to re-add panels that were already added, I get a null exception error and I cannot figure out why.


      ** The coverflow control can be obtained here: http://dougmccune.com/blog/2007/11/03/coverflow-flex-component/


      Here is a sample application that demonstrates my situation:

                  import mx.events.FlexEvent;
                  protected function btnAdd_clickHandler(event:MouseEvent):void
                          //reference the hidden Panel 'B'
                          var thePanel:DisplayObjectContainer = panelGroup.getChildByName("B") as DisplayObjectContainer;
                          //add new panel to coverflow
                          //reset the scroll bar positioning
                          scrollbar.maxScrollPosition=coverflow.numChildren - 1;                   
                  protected function btnClear_clickHandler(event:MouseEvent):void


                  protected function application1_initializeHandler(event:FlexEvent):void


          <mx:VBox width="100%">
              <containers:CoverFlowContainer id="coverflow"
              <mx:HScrollBar id="scrollbar" width="100%" pageSize="1"
                             scroll="coverflow.selectedIndex = Math.round(scrollbar.scrollPosition)" />       
              <mx:Button id="btnAdd" label="Add a panel" click="btnAdd_clickHandler(event)" color="black"/>
              <mx:Button id="btnClear" label="Clear" click="btnClear_clickHandler(event)" color="black" />
              <mx:Panel visible="false" id="panelGroup" width="0" height="0" includeInLayout="false" >
                  <mx:Panel id="A" name="A" width="300" height="475" title="Profile 1" backgroundColor="52326" includeInLayout="false">
                      <mx:Form label="A" >
                          <mx:Label text="Hello A" width="120" fontSize="14" color="black" />
                  <mx:Panel id="B" name="B" width="300" height="475" title="Profile 2" backgroundColor="16776960" includeInLayout="false">
                      <mx:Form label="B" >
                          <mx:Label text="Hello B" width="120" fontSize="14" color="black" />
                  <mx:Panel id="C" name="C" width="300" height="475" title="Profile 3" backgroundColor="16750848" includeInLayout="false">
                      <mx:Form label="C" >
                          <mx:Label text="Hello C" width="120" fontSize="14" color="black" />







      ^ When this example app loads, it removes all children from the coverflow and loads two panels (A and C). That works fine.


      Now I can add Panel B to the coverflow when I click Add. You can see it popup in the background, in the order the panels were loaded. That works fine too.


      Now if I hit the "clear" button and remove all from the coverflow, and then try to add Panel B back (by clicking add a 2nd time), it throws a NULL reference object exception.


      Now the thing that bothers me is if I create a Watch Expression while Debugging, I can still see Panel B and all its properties, like it still exists hidden in the "panelGroup" panel, I hide them away in. Panel B does not appear to be NULL when debugging.


      Why cant I add Panel B twice? I dont even need to add them twice in one session, I just need to re-add Panel B after the coverflow is cleared. Why cant I do that? Does it have something to do with the CreationPolicy? or am I missing something, like destroying my Panel objects after adding them to the coverflow?


      Why is Panel B coming up as NULL, when I can see it in Watch Expression? Is it no longer a part of panelGroup, and instead has a new parent of coverflow? Am I changing the parent relationship, by adding it to the coverflowl? Does it move to coverflow parent instead of panelGroup parent?


      Any help into this situation is appreciated. Thanks.