Skip to main content
Participant
April 27, 2011
Question

Need help please. Error #1009

  • April 27, 2011
  • 3 replies
  • 537 views

Hey guys,

I need a little help with my AS3 game that I am creating for my class.  I got this error when I tried to play it.  I can't really figure it out.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at mazeGame/init()

at mazeGame()

Here's my code where I am having problems:

public class mazeGame extends Sprite {

          

          private var theMaze:Maze;

          private var theKnight:Knight;

          private var theGoal:Goal;

          private var theUp:Boolean = false;

          private var theDown:Boolean = false;

          private var theLeft:Boolean = false;

          private var theRight:Boolean = false;

          public function mazeGame() {

                    init();

               

          }

          

          private function init():void {

               theMaze = new Maze();

               addChild(theMaze);

               theMaze.x = stage.stageWidth/2;

               theMaze.y = stage.stageHeight/2;

               

               theKnight = new Knight();

               addChild(theKnight);

               theKnight.x = stage.stageWidth/2 - 100;

               theKnight.y = 20;

               theKnight.scaleX = theKnight.scaleY = .1;

               

               theGoal = new Goal;

               addChild(theGoal);

               theGoal.x = stage.stageWidth - 30;

               theGoal.y = stage.stageHeight/2;

               

If you guys need the rest of my code let me know please.  Thanks again.

This topic has been closed for replies.

3 replies

relaxatraja
Inspiring
April 28, 2011

Look at the sample below and make the correction accordingly:

//Stage  Reference
        public static var stage:Stage;

public function Main():void{

          //Assigning the stage for future ref
            Main.stage = this.stage;

             _sw=stage.stageWidth;
            _sh=stage.stageHeight;

}

Inspiring
April 27, 2011

Firs, when you paste your code, please make sure it is readable - yours have &nbsp all over the plase.

As for the error - first thing that will cause it that at class instantiation stage is not available. stage is available ONLY to DisaplyObjects are are placed on display list with addChild() method. To remedy that - either listen to ADDED_TO_STAGE event and only then call init() or pass stage reference to the class instance. First approach is preferable.

Ned Murphy
Legend
April 27, 2011

It might be a case where you are trying to target the stage, but if the object is not yet added to the stage, the stage does not yet exist for it.  I think you need to wait before trying to address the stage.

Try searching Google using "AS3 stage null in class" and you should find discussions with how you should be able to overcome this.  Here is one result from that search where the responses might help you resolve this...

http://stackoverflow.com/questions/1532955/help-null-object-error-when-using-stage-addeventlistener-scrollbar