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

      Hello,

       

      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:

      <s:Application
          xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:containers="com.dougmccune.containers.*"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          initialize="application1_initializeHandler(event)"
          >
         
          <fx:Script>
              <![CDATA[ 
                  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
                          coverflow.addChild(thePanel);
                     
                          //reset the scroll bar positioning
                          scrollbar.maxScrollPosition=coverflow.numChildren - 1;                   
                     
                  }
                 
                  protected function btnClear_clickHandler(event:MouseEvent):void
                  {
                      coverflow.removeAll();
                  }

       

                  protected function application1_initializeHandler(event:FlexEvent):void
                  {
                      coverflow.removeAll();
                      coverflow.addChild(A);
                      coverflow.addChild(C);
                  }

       

          ]]>
          </fx:Script>
         
          <mx:VBox width="100%">
             
              <containers:CoverFlowContainer id="coverflow"
                                             width="100%"
                                             height="100%"
                                             horizontalGap="40"
                                             borderStyle="inset"
                                             backgroundColor="0x000000"
                                             segments="6"
                                             reflectionEnabled="false"
                                             visible="true"
                                             />               
              <mx:HScrollBar id="scrollbar" width="100%" pageSize="1"
                             scrollPosition="{coverflow.selectedIndex}"
                             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:Form>
                  </mx:Panel>
                 
                  <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:Form>
                  </mx:Panel>
                 
                  <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" />
                      </mx:Form>
                  </mx:Panel>
                 
              </mx:Panel>       
             
          </mx:VBox>

       

      </s:Application>

       

       

       

       

      ^ 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.