Jingwei
Jingwei

Reputation: 115

Flex 4 Modules Error #1009

I'm upgrading my applications from flex 3 to flex 4.5 and encountered an error. It happened when a component was dynamically added into a module. The codes that can simulate the error have been extracted as below.

TestError.mxml

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

<mx:Script>
    <![CDATA[
        import mx.events.ModuleEvent;

        protected function ml_readyHandler(event:ModuleEvent):void
        {
            trace("Module finished loading");
            (ml.child as TestErrorModuleInterface).testTracing();
        }
    ]]>
</mx:Script>

<mx:ModuleLoader id="ml" url="TestErrorModule.swf" ready="ml_readyHandler(event)"/>

</mx:Application>

TestErrorModule.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" 
       layout="absolute" width="100%" height="100%"
       implements="TestErrorModuleInterface">
<mx:Canvas id="_box">
    <mx:Label text="HERE IS MY TEST MODULE" x="142" y="133"/>
</mx:Canvas>

<mx:Script>
    <![CDATA[

        public function testTracing():void
        {
            trace("This is a method in the Test Module");
            var comp:TestErrorModuleComp = new TestErrorModuleComp();
            _box.addChild(comp);
        }
    ]]>
</mx:Script>

</mx:Module>

TestErrorModuleInterface.as

package {
public interface TestErrorModuleInterface {
        function testTracing():void;
    }
}

TestErrorModuleComp.mxml

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

    <mx:Label text="element" width="80" />

</mx:Canvas>

And this is the detailed error stacktrace:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at TestErrorModule/testTracing()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestErrorModule.mxml:16]
at TestError/ml_readyHandler()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestError.mxml:12]
at TestError/__ml_ready()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestError.mxml:17]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at mx.modules::ModuleLoader/moduleReadyHandler()[E:\dev\hero_private\frameworks\projects\mx\src\mx\modules\ModuleLoader.as:465]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at ModuleInfoProxy/moduleEventHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\modules\ModuleManager.as:1149]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at ModuleInfo/readyHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\modules\ModuleManager.as:793]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::FlexModuleFactory/update()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:535]
at mx.core::FlexModuleFactory/docFrameHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:681]
at mx.core::FlexModuleFactory/docFrameListener()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:131]

Thanks a lot!

Upvotes: 0

Views: 775

Answers (1)

Constantiner
Constantiner

Reputation: 14221

Try to use something like:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" 
       layout="absolute" width="100%" height="100%"
       implements="TestErrorModuleInterface">
<mx:Canvas id="_box">
    <mx:Label text="HERE IS MY TEST MODULE" x="142" y="133"/>
</mx:Canvas>

<mx:Script>
    <![CDATA[

        public function testTracing():void
        {
            trace("This is a method in the Test Module");
            if (initialized)
                addComp();
            else
                callLater(addComp);
        }

        private function addComp():void
        {
            var comp:TestErrorModuleComp = new TestErrorModuleComp();
            _box.addChild(comp);
        }
    ]]>
</mx:Script>

</mx:Module>

The problem is your _box isn't exist at the moment of module loading. You should wait module initialization first. callLater() here is an ugly first step. It is better to use component's invalidation then (in the process of refactoring).

Upvotes: 1

Related Questions