4 Replies Latest reply on Oct 2, 2009 7:34 AM by NeedaFlexpert

    Update Text Property Inside Other Containers

    NeedaFlexpert

      Calling all Flexpertz!!

       

      Hi , I am trying to update a textarea text property with ActionScript that I have nested inside a vbox, inside a panel, inside a canvas.  While debugging, I notice that the textarea object is null if its nested inside other containers.  But if I allow it to be outside, I can set the text just fine.

       

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at ....

       

       

      Any ideas?

        • 1. Re: Update Text Property Inside Other Containers
          Gregory Lafrance Level 6

          The following code shows three ways to change the text of the TextArea:

           

          - In ActionScript in a Button inline "click" handler

          - In ActionScript in a Button "click" handler in a Script tag

          - In ActionScript without using ID

           

          If this post answered your question or helped, please mark it as such.

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                  <![CDATA[
                      import mx.core.Container;
                      private function setByID():void{
                          myTxt.text = "Set by ID - ActionScript in Script Tag";
                      }
                      private function setWithoutIDHandler(evt:MouseEvent):void{
                          setWithoutID(evt.currentTarget.parent as Container);
                      }
                      private function setWithoutID(container:Container):void{
                          for each(var obj:Object in container.getChildren()){
                              if(obj is Container){
                                  setWithoutID(obj as Container);
                              }else if(obj is TextArea){
                                  TextArea(obj).text = "Set without ID";
                              }
                          }
                      }
          ]]>
          </mx:Script>
              <mx:VBox>
                  <mx:Panel>
                      <mx:Canvas>
                          <mx:TextArea id="myTxt" text="initial text" width="800"/>
                      </mx:Canvas>
                  </mx:Panel>
              </mx:VBox>
              <mx:Button label="Set by ID - Inline ActionScript" click="myTxt.text='Set by ID - Inline ActionScript'"/>
              <mx:Button label="Set by ID - ActionScript in Script Tag" click="setByID();"/>
              <mx:Button label="Set without ID" click="setWithoutIDHandler(event);"/>
          </mx:Application>
          
          
          • 2. Re: Update Text Property Inside Other Containers
            NeedaFlexpert Level 1

            Thanks ,

             

            I should have been a little more specific with my explanation.

             

            Try this code .. click button 1, then button 2.  Clicking button 2 results in the null object reference error.  That is what I'm trying to fix.

             

            Is there something else I need to do to "activate" the 2nd text area?

            BTW, once you get the first error, you can click button 2 again and then it works.

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

            <mx:Script>
              <![CDATA[
               private function testclick()
               {
                canvas.visible = true;
                tabnav.selectedChild = vb1;   
                ta1.text = "TA1";
               } 
               private function testclick2()
               {
                canvas.visible = true;
                tabnav.selectedChild = vb2;   
                ta2.text = "TA2";
               } 
              ]]>
            </mx:Script>


            <mx:Button x="43" y="529" label="Button" id="testbtn" click="testclick()"/>
            <mx:Button x="120" y="530" label="Button" id="testbtn2" click="testclick2()"/>
            <mx:Canvas id="canvas" width="290" height="232" visible="false">
              <mx:Panel id="panel" width="270" height="212" layout="absolute" styleName="Panel" 
              title="Details" y="0" cornerRadius="20" x="0" backgroundColor="#82BB7C" backgroundAlpha="1.0">
              <mx:TabNavigator id="tabnav" right="0" top="0" backgroundAlpha=".5" width="250" height="170">
               <mx:VBox label="vb" id="vb1" width="250" height="150">
                <mx:TextArea id="ta1" width="30" height="30"/>
               </mx:VBox>
               <mx:VBox label="vb2" id="vb2" width="250" height="150">
                <mx:TextArea id="ta2" width="30" height="30"/>
               </mx:VBox>
              </mx:TabNavigator>  
              </mx:Panel>
            </mx:Canvas>
            </mx:Application>

            • 3. Re: Update Text Property Inside Other Containers
              Gregory Lafrance Level 6

              You want creationPolicy="all", though its use is considered anti-best practice. Best practice has you initializing necessary components on creationComplete for the TabNavigator, though that can get messy.

               

              If this post answers your question or helps, please mark it as such.

               

               

              <mx:TabNavigator id="tabnav" right="0" top="0" backgroundAlpha=".5" width="250" height="170"
                  creationPolicy="all">

              • 4. Re: Update Text Property Inside Other Containers
                NeedaFlexpert Level 1

                Thank you Sir, you Truly are a FlexPert!!

                 

                That worked just fine.

                 

                Andrew