2

I am making a website in Flash, coded in flashbuilder. Whenever I try to export my code I get the same error again and again (TypeError = see below).

I think the problem has something to do with the stage of my project. Whenever I change the var stageMiddenX = (stage.stageWidth / 2); into var stageMiddenX = 512;, the code works. but I wan't the var to be dynamic.

TypeError Error #1009: cannot access a property or method of a null object reference at main()

package {
import flash.display.MovieClip;

  public class main extends MovieClip{
    var stageMiddenX = (stage.stageWidth / 2);
    var stageMiddenY = (stage.stageHeight / 2);
    private var object1:Object1 = new Object1();
    private var object2:Object2 = new Object2();
    private var object3:Object3 = new Object3();
  }
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
W4cko
  • 23
  • 1
  • 3
  • W4cko, I fixed your question, but *please for the love of all that is holy*, learn how to format correctly on Stack Overflow. [Read this](http://stackoverflow.com/editing-help). – Michael Petrotta Sep 07 '11 at 03:09
  • @Michael Petrotta : Thx for the help with the format :P – W4cko Sep 07 '11 at 10:20

2 Answers2

6

The issue here is that stage is not yet available at the time you are requesting it.

You'll want to wait until the Event.ADDED_TO_STAGE event is fired before attempting to acccess stage.

package {
    import flash.display.MovieClip;

    public class main extends MovieClip{
        private var object1:Object1 = new Object1();
        private var object2:Object2 = new Object2();
        private var object3:Object3 = new Object3();
        private var stageMiddenX:Number;
        private var stageMiddenY:Number;

        public function main(){
            if(stage) init(null);
            else addEventListener(Event.ADDED_TO_STAGE, init)
        }

        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            stageMiddenX = (stage.stageWidth / 2);
            stageMiddenY = (stage.stageHeight / 2);
        }
    }
}
Marcela
  • 3,728
  • 1
  • 15
  • 21
  • 2
    This process isn't required if the class above is the document class.. Which I'm not sure if the example is.. So +1 anyway! – Marty Sep 07 '11 at 03:22
  • I thought I'd recalled having issues with trying to access `stage` "too early" (even in the document class). However, I just did a quick check and found that you are correct. I would vote up your answer if I had enough rep. – Marcela Sep 07 '11 at 03:27
  • Marcela is right on this one. Document classes are no exception. – Jacksonkr Sep 07 '11 at 04:08
  • 1
    @Jackson Have you actually tried accessing stage from within the constructor of a doc class? Works fine. – Marty Sep 07 '11 at 04:26
  • In my previous flash project I used my original AS code and it worked. In this project I had difficulties with the exact same code :s strange don't you think .. BUT the problem is solved thanks to you guys. THX !! – W4cko Sep 07 '11 at 13:31
1

Put the stuff accessing stage into a constructor (assuming this is your document class)..

package
{
    import flash.display.MovieClip;

    public class main extends MovieClip
    {
        public var stageMiddenX:int;
        public var stageMiddenY:int;

        private var object1:Object1 = new Object1();
        private var object2:Object2 = new Object2();
        private var object3:Object3 = new Object3();

        public function main()
        {
            stageMiddenX = stage.stageWidth / 2;
            stageMiddenY = stage.stageHeight / 2;
        }
    }
}
Marty
  • 39,033
  • 19
  • 93
  • 162