What is the null object error with this AS3 code to load external swf?

I get an error with a null object when I add a mouse event listener for the login button. (Look at the comments in the constructor)

I am using Flash CS6, and objects such as logInbutton and screen_log_in are instance names from the .fla file. This is the .as file.

Error:

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

My AS3 code:

 package actions { import flash.display.MovieClip; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IEventDispatcher; import flash.net.URLRequest; import flash.display.Loader; import fl.motion.MotionEvent; import flash.events.MouseEvent; public class indexPage extends MovieClip { public function indexPage():void { loadSWF("http://mathlympics.cu.cc/loginsystem.swf"); //THIS IS THE LINE WHICH IS CAUSING THE ERROR //WHEN I COMMENT IT OUT THE ERROR IS GONE logInButton.addEventListener(MouseEvent.CLICK, goToLogIn); } var _swfLoader:Loader; var _swfContent:MovieClip; public function loadSWF(path:String):void { var _req:URLRequest = new URLRequest(); _req.url = path; _swfLoader = new Loader(); setupListeners(_swfLoader.contentLoaderInfo); _swfLoader.load(_req); } function setupListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.COMPLETE, addSWF); } function addSWF(event:Event):void { event.target.removeEventListener(Event.COMPLETE, addSWF); event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF); _swfContent = event.target.content; screen_log_in.addChild(_swfContent); } function unloadSwf():void { _swfLoader.unloadAndStop(); screen_log_in.removeChild(_swfContent); _swfContent = null; } function goToLogIn(e:MouseEvent):void { unloadSwf(); screen_log_in.loadSWF("http://mathlympics.cu.cc/loginsystem.swf"); } function goToRegister(e:MouseEvent):void { unloadSwf(); screen_log_in.loadSWF("http://mathlympics.cu.cc/register.swf"); } } } 
+1
source share
2 answers

I am only going to answer my question for future visitors. The cause of the problem that I noticed was able to figure out, but I figured out how to get around this. I just copy my code from the document class to frames, and it works fine there.

0
source

You cannot access the scene until the stage is available.

 public function indexPage():void { addEventListener(Event.ADDED_TO_STAGE,init) } public function init(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE,init) loadSWF("http://mathlympics.cu.cc/loginsystem.swf"); //THIS IS THE LINE WHICH IS CAUSING THE ERROR //WHEN I COMMENT IT OUT THE ERROR IS GONE logInButton.addEventListener(MouseEvent.CLICK, goToLogIn); } 
+2
source

All Articles