A problem I ran into today: I have an swf file that I need to load into a main file, but I kept hitting the following issue: after calling loadSwf(“xxxx.swf”), it throws TypeError: Error #1009: Cannot access a property or method of a null object reference.
The loading code looks like this: private function loadMainSwf(url):void { var urlR:URLRequest=new URLRequest(url); containtLoader.unload(); containtLoader.load(urlR); containtLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadHandler); containtLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompHandler); } private function loadHandler(e:ProgressEvent):void { gLoad=e.target.bytesLoaded; gAll=e.target.bytesTotal; per=Math.floor(gLoad / gAll * 100); percent=per + “%”; perString.text=percent; } private function loadCompHandler(e:Event):void { containtLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loadHandler); containtLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadCompHandler); addChild(containtLoader); }
The error occurs in the constructor of the sub-movieclip’s binding class, and in the constructors of every other class inside that sub-movieclip. I did some searching this morning. There are two possible causes:
Possibly because the loaded swf has named instances on its stage. I haven’t tested this. But based on past experience, there are two possibilities: 1.
ldris null; 2. the loaded swf has a binding class and has named instances on its stage. Solutions: 1. Make sure 1.swf is in the same directory as the current swf; 2. if the loaded swf has a binding class and has named instances on its stage, remove the instance names of all named instances on the stage (main timeline). — Blueidea BBS
After mumbling to myself for a while, I found the culprit: stage. Here’s the situation: the constructor of the sub-movieclip’s binding class contains
stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT;
When I test the sub-movieclip on its own, everything is fine — when the constructor runs, trace(stage) gives [object Stage]. That means the sub-movieclip can already access stage while its constructor is executing. But when the sub-movieclip is loaded into the main movie, things are different: when the constructor runs, trace(stage) is null. This means that when a sub-movieclip is loaded, its constructor runs first and only then does it negotiate with the “stage”. While the constructor is executing, it has no idea what stage even is (my personal take). So I moved the stage operations in the sub-movieclip into the ADDED_TO_STAGE event, and it worked.
public function MousebombV9Index() { init(); } private function init():void { //***other statements…. this.addEventListener(Event.ADDED_TO_STAGE,addStageHandler); } private function addStageHandler(e:Event):void { //align to the top-left of the stage stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; }
This way it works both when used standalone and when loaded as a sub-movieclip.