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

**URL:** https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369
**Category:** Uncategorized
**Created:** [June 15, 2008, 5:28am UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369 "2008-06-15T05:28:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 15, 2008, 5:28am UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369/1 "2008-06-15T05:28:54Z")

</div>

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:  
\*\*

```auto

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:

```auto

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.

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [June 15, 2008, 5:45am UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369/2 "2008-06-15T05:45:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [June 15, 2008, 5:27pm UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369/3 "2008-06-15T17:27:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [June 15, 2008, 5:50pm UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369/4 "2008-06-15T17:50:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![nikefido](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nikefido](https://forum.kirupa.com/u/nikefido)
#### Post date: [June 15, 2008, 8:43pm UTC](https://forum.kirupa.com/t/how-to-deal-with-1009-errors-when-loading-a-child-swf-run-with-a-document-class/263369/5 "2008-06-15T20:43:30Z")

</div>

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?
