10 Replies Latest reply on Jul 30, 2011 10:17 PM by Flex harUI

    How to use a variable out of the nested <fx:component> tag?

    flexx0r

      Hi flex community,

       

      I am trying to access the variable house from the component tag. But I always get an this error when I press the button:

       

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

       

      here is my code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                             xmlns:s="library://ns.adobe.com/flex/spark"
                             xmlns:mx="library://ns.adobe.com/flex/mx">
          <fx:Script>
              <![CDATA[
                 
                  private var newWindow:MyNativeWindow;
                  public var house:String = "wooden house";
                 
                  private function openNewWindow():void {
                      if (newWindow != null) newWindow.close();
                      newWindow = new MyNativeWindow();
                      newWindow.width = 200;
                      newWindow.height = 200;

       

                      try
                      {                   
                          newWindow.open();
                      }
                      catch (err:Error)
                      {           
                          trace(err.message);
                      }
                  }
                 
              ]]>
          </fx:Script>   
         
          <fx:Declarations>
              <fx:Component className="MyNativeWindow">
                  <mx:Window horizontalAlign="center" verticalAlign="middle" backgroundColor="blue">
                      <fx:Script>
                          <![CDATA[                   
                              protected function button1_clickHandler(event:MouseEvent):void
                              {
                                  trace(outerDocument.house);
                              }                       
                          ]]>
                      </fx:Script>
                      <mx:Button label="Yeah" click="button1_clickHandler(event)" />
                      <mx:Button label="close" click="this.close()"/>               
                  </mx:Window>
              </fx:Component>   
          </fx:Declarations>   
         
          <s:VGroup horizontalCenter="0" top="10">
              <s:Button label="Open New Window" color="black" click="openNewWindow()"/>
          </s:VGroup>
      </s:WindowedApplication>