PDA

View Full Version : How to deal with #1009 Errors when loading a child SWF run with a document class.



Anogar
June 15th, 2008, 01:28 AM
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 #1009 error most likely because you are attempting to access the stage before the loader object that contains the child swf has been added to the stage - so the stage object is returning null. 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 init() 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 init() 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.

TheCanadian
June 15th, 2008, 01:45 AM
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.

Anogar
June 15th, 2008, 01:27 PM
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.

TheCanadian
June 15th, 2008, 01:50 PM
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.

nikefido
June 15th, 2008, 04:43 PM
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?