First I’ve encountered this problem some days ago. In fact, this is not a problem. You just need too understand the situation. The stage property is an instance of Stage class and every Stage object has it, but only when it is added to stage with addChild() method. It means that we need to listen when has been added to stage our object.
// Main.as
// main class which is attached to stage
package
{
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite{
public function Main():void {
var sprite:StageTest = new StageTest();
addChild(sprite)
}
}
}
// StageTest.as
// external class
package
{
import flash.display.Sprite;
import flash.events.Event;
public class StageTest extends Sprite{
public function StageTest():void {
/*
trace(stage.stageHeight);
Here it will return an error because it is not added to the stage yet.
The Error is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at StageTest()
at Main()
*/
addEventListener(Event.ADDED_TO_STAGE, addedHandler);
}
private function addedHandler(event:Event){
trace(stage.stageHeight);
}
}
}