6 Replies Latest reply on Apr 12, 2010 1:23 PM by Darrell Loverin

    Accessing parent application from module (New to Flex)

    mbskels

      Hi

       

      I have been looking into the use of modules in Flex using module loader.


      Module loads ok and I can call methods in the child module. Run into a problem though when I try to get a reference to the application from within the child module using parentApplication as suggested in Adobe documentation

       

       

      At that point I get Error #1009: Cannot access a property or method of a null object reference.

       

      Problem is in UIComponent.as where systemManager.document appears to be null

       

          public function get parentApplication():Object
          {
              // Look for the SystemManager's document,
              // which should be the Application.
              var o:Object = systemManager.document;

       


      I have detailed below a simplified example of my code.

       

      Appreciate if someone could point out where I have gone wrong?

       

      TIA

       

      Malcolm

       

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
          <mx:Script>
              <![CDATA[
                 
                  private function option1Handler():void
                  {
                      loadModule("Module1.swf");
                  }

       

                  private function loadModule(moduleUrl:String):void
                  {
                      var url:String = moduleLoader.url;
                      if (url == moduleUrl)
                          return;
                      if (url != null)
                          moduleLoader.unloadModule();
                      moduleLoader.url = moduleUrl;                   
                  }

       

                  private function moduleLoadedListener():void
                  {
                              (moduleLoader.child as Module1).initModule();
                  }
                 
                  public function moduleClosing():void
                  {
                      trace("message from module 1");
                  }

       

              ]]>
          </mx:Script>
          <mx:ModuleLoader id="moduleLoader" ready="moduleLoadedListener() "/>
          <mx:Button x="114" y="98" label="Menu Option 1" id="menuOpt1" click="option1Handler()"/>
         
      </mx:Application>

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="400" height="300" >
          <mx:Script>
              <![CDATA[
                             
                  public function initModule():void
                  {
                          trace("In Module 1");

                          // crashes here
                          this.parentApplication.moduleClosing();   
                  }
              ]]>
          </mx:Script>

       

      </mx:Module>

        • 1. Re: Accessing parent application from module (New to Flex)
          Gregory Lafrance Level 6

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

           

          This page may help:

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=modular_7.html

          • 2. Re: Accessing parent application from module (New to Flex)
            mbskels Level 1

            Hi Greg,

             

            That is the article I used to investigate the use of modules and based my example on.

            It uses parentApplication to access the parent application but unfortunately in my example this causes the null object error specified in my post.

             

            I imported the Adobe example into my Flex environment and it runs ok so there is obviously a fault with my code - I just cannot find it.

             

            Malcolm

            • 3. Re: Accessing parent application from module (New to Flex)
              Gregory Lafrance Level 6

              Maybe you can use mx.core.Application.application instead.

               

              Even better is to use custom events to pass data.

               

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

              1 person found this helpful
              • 4. Re: Accessing parent application from module (New to Flex)
                mbskels Level 1

                Thanks Greg. mx.core.Application.application does work but I hoped to avoid that.

                 

                Still intrigued why parentApplication line of code crashes in the original module so

                I tried reworking the module as below and this runs fine so now totally lost for an explanation.

                 

                <?xml version="1.0" encoding="utf-8"?>
                <!-- modules/ChartChildModule.mxml -->
                <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
                height="100%" >
                    <mx:Script>
                        <![CDATA[
                                       
                            public function initModule():void
                            {
                                    trace("In Module 1");
                                       
                            }
                        ]]>
                    </mx:Script>
                    <mx:Button id="b1" x="114" y="118" label="Close The Module" click="parentApplication.moduleClosing()" />

                 

                </mx:Module>

                 

                 

                I agree that custom event would be better and I started out with that approach but the event was not detected in the parent application. I have used custom events before so I assumed it was failing because of something to do with modules and that is when I tried the parentApplication as described in Adobe docs. I will give custom event another try and if it fails again I will post the code.


                Malcolm

                • 5. Re: Accessing parent application from module (New to Flex)
                  saisri2k2 Level 4

                  Do you still know how to solve this?

                   

                  i'm doing the following.. but it still shows that error at the systemManager.

                   

                  if(_modLoader){
                                      _modLoader.addEventListener(ModuleEvent.READY,readyMod);
                                      _modLoader.addEventListener(ModuleEvent.ERROR,loadError);
                                      _modLoader.addEventListener(ModuleEvent.PROGRESS,loadProgress);
                                      _modLoader.addEventListener(ModuleEvent.UNLOAD,unload);
                                      _modLoader.applicationDomain = ApplicationDomain.currentDomain;
                                      
                                      _modLoader.addEventListener(FlexEvent.PREINITIALIZE,mLoaderPreinit);
                                      _modLoader.url = url;
                                      _modLoader.loadModule();
                                 } 
                            }
                            
                            private function mLoaderPreinit(evt:FlexEvent):void{
                                 _modLoader.moduleFactory = Application.application.systemManager;
                            }
                            
                            private var oMod:IHygeiaModule;
                            public function readyMod(evt:ModuleEvent):void{
                                 /* var oMod:IHygeiaModule = evt.module.factory.create() as IHygeiaModule;
                                 oMod.initModule(Application.application as IHygeiaApp);  */
                                 oMod = _modLoader.child as IHygeiaModule;
                                 
                                 _modLoadFunc(_currModule,oMod);
                            }
                  

                   

                   

                  The readyMod function will be called i do not call any method after this.. but the systemManager is still null Should i be doing something after the ready event so tht the module get the systemmanger handle?

                  • 6. Re: Accessing parent application from module (New to Flex)
                    Darrell Loverin Level 4

                    In the application don't reference the module's class name directly, "Module1". This causes "Module1" and all of its dependencies to be linked into the application, which is not what you want. Trying using an interface instead.

                     

                     

                    -Darrell