Copy link to clipboard
Copied
from my documentclass i got a isntance of the my view class in there i got a instance of navigation class extends Sprite
I got a utilz.as file with all static public functions like this one
alignToCenter(obj:DisplayObject):vid
{
obj.x = obj.stage.stageWidth/2;
}
Utilz.alignToCenter(instance of navigation class)
I get a error stage is null reference..
What is a easy way to fix that?
Main is your document class?
(and don't nest named functions.)
Copy link to clipboard
Copied
If i want to create a ViewModel class
and it should have a stage reference
like
package mvc.view
{
import flash.display.Stage;
public class ViewModel extends Sprite
{
private var s:Stage = new Stage() <--- btu this is really the current stage ![]()
public function ViewModel()
{
}
public function update():void
{
}
}
}
This would be one idea right?
Copy link to clipboard
Copied
A DisplayObject's stage property becomes available when the instance is added to the display list.
When trying to access the stage property too soon, it'll throw an error.
Try listening for the ADDED_TO_STAGE event in you custom class, at which time the stage property will be available for use.
Copy link to clipboard
Copied
Yes, null reference error almost always means the thing's not loaded yet. Adding an event listener is the most direct solution. I usually find it easiest just to eliminate any stage-positioning reference that isn't in the main class.
Copy link to clipboard
Copied
in my main class before i do anything i do :
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = "TL";
trace(stage.stageWidth);
init();
}
}
why i get 0 on my trace? stage is there ![]()
Copy link to clipboard
Copied
Main is your document class?
(and don't nest named functions.)
Copy link to clipboard
Copied
Yeah!
Copy link to clipboard
Copied
I am pretty sure this is what is going on:
1) Your main/document class brings in another class--called either Navigation.as or Utilz.as (I can't tell), and inside this other class you are centering an object within that class's own stage.
2) But your Utilz.as (or Navigation) class, including all the subclasses and objects inside it, is not fully loaded yet when the Flash engine tries to center an object...
3) So Flash gives a null reference error!
4) Your event listener appears to be in your main class, but your problem is in your utilz or navigation class, and that's where you have to put your listener. If you are going to center an object within a subclass, you have to make sure it is loaded first.
Finally: Try commenting out // that stage-centering property and see if you get a different result.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more