0
votes

I am working with Flash Professional IDE. Referencing the classes below, if I set the class property of the FLA to testingsub1 (ignoring Main) the code works fine. But if I set the class to Main the code dies at stage.addChild(container); of testing with "TypeError: Error #1009: Cannot access a property or method of a null object reference.".

This goes against my understanding that the stage is global and always available to have display objects added directly. Any insight and solution?

package {
    //import com.idtlw.iso.utils.Const;
    import flash.display.*;

    public class testingsub1 extends testing {

        public function testingsub1() {
            trace("initializing testing sub 1");
            var container:Sprite=new Sprite();

            var test1:Sprite=new Sprite();

            container.addChild(test1);
        }
    }
}

package {
    import flash.display.*;

    public class testing extends Sprite {

        public function testing() {
            trace("initializing testing");
            var container:Sprite=new Sprite();

            var test1:testreg1=new testreg1();

            container.addChild(test1);

            stage.addChild(container);
        }
    }
}

package 
{

        import flash.display.*;

        public class Main extends Sprite
        {
                public function Main ()
                {
                        var test1:Sprite=new Sprite();
                        stage.addChild(test1);

                        var tester:testingsub1=new testingsub1();
                }
        }
}
2
The stage is not globally accessible. The stage property of a DisplayObject is null unless it is attached to the display list hierarchy of the stage. - prototypical
I’d argue that—other than the main display object—nothing should ever need to access stage directly. We are talking about components that could be nested in many possible situation; a component shouldn’t be able to add another component to the stage directly. - poke

2 Answers

0
votes

If you'd dwell in FlashDevelop tutorials, you'll find a tricky line of code:

if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE,init);

This means that stage is not always available even to the Main class. Add this line, create a function init(e:Event=null):void putting any code that requires access to stage there, and you're set.

About your headline question - no class have stage available in the constructor by design, even if an object is placed to the stage at design time and it has a custom constructor, you cannot get valid reference to stage within that constructor. The stage property is filled only after the object has been added to a display list of an object that has it filled, or to stage.

0
votes

As prototypical already mentioned in his comment, Stage is just a convenience property of DisplayObjects, and is only accessible if that object is on the display list.

In your testing class ctor you are accessing it's stage object. This will ALWAYS be null until it has been added to the display list. You do not add it until after your constructor has finished, hence the NRE. The correct way to handle is this is register an event listener in your ctor to listen for ADDED_TO_STAGE and then add the container.

I would argue however that your intend approach isn't a good idea. DOs should normally be self contained and not accessing the display list directly via stage. It's much better practice to add the container to your testing class (or just use the class as the container) and have your main add that class to the display list where appropriate.