How to deal with #1009 Errors when loading a child SWF run with a document class

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.

Great post, this should be stickied. Actually there is a FAQ thread, maybe this would belong in there OR at least a link from there to here.

If we were really smart we’d update the FAQ thread because theres about a billion questions that are asked a billion times a day and I’m sure everyone doesn’t feel like typing the same thing a quintillion times. I’ve been away from Flash for a while now but I might start adding/organizing the FAQ thread and other people can add as well! It seems from the responses I just read 2 seconds ago that it has helped a few people.

Perfect, yeah if you re-organize the FAQ thread put a link to this in there so that we can find it and send other people to it when they get this issue, since it seems to come up a lot.

I’ll work on that over the next few days. I’m gonna try to add AS3 examples to all of the answers and hopefully start adding new ones in a while.

does running stage.stageWidth in the init function of such a loaded swf return the stage width of the loaded swf or of the parent swf?