This is an issue that I see almost every day, so I figured I’d make a general purpose post about it that we can point to.
Here is the issue:
You are receiving a [FONT=Courier New]#1009 error[/FONT] most likely because you are attempting to access the [FONT=Courier New]stage [/FONT]before the loader object that contains the child swf has been added to the stage - so the stage object is returning **[FONT=Courier New]null. [/FONT]**You cannot access the properties of a null object.
Here is the way to fix it. Remove all of the code from your constructor - and place it into an [FONT=Courier New]init()[/FONT] function. Here is an example:
**Your original code:
**
package
{
import flash.display.Sprite;
public class MyDocumentClass extends Sprite
{
public function MyDocumentClass()
{
trace(stage.stageWidth); //this is going to throw a #1009 error - there is no stage yet.
}
}
}
That’s going to give you an error - the stage doesn’t exist yet. The solution is to take all relevant code out of the constructor, and place it into an [FONT=Courier New]init()[/FONT] function that is called when the ADDED_TO_STAGE function is fired, as follows:
package
{
import flash.display.Sprite;
import flash.events.*;
public class MyDocumentClass extends Sprite
{
public function MyDocumentClass()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init():void
{
trace(stage.stageWidth); //this will not throw an error when loaded
}
}
}
Hope that helps. I see this question a lot, so please feel free to point anyone asking it to this thread instead of repeating yourself over and over.
Of course, other things can cause the #1009 error, but if you have a perfectly good SWF that is throwing errors when loaded into a container, this is a great place to start.