Alfonso Rubalcava
Alfonso Rubalcava

Reputation: 2247

Strange behavior from a dictionary

In a class define a dictionary, which need restart according to some application events. If the application window has focus, so good ... but if not so I get:

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

This is the relevant code:

private var seleccion:Dictionary= new Dictionary();

private function myfunction():void{
    trace(stage);//it is always [object Stage]
    if(seleccion){ //always there
        trace(seleccion);////it is always [object Dictionary]
        seleccion= new Dictionary();//if have focus, ok. If not i get error #1009
    }
}

I simplified the code to leave only that relevant to the question (I think) ... Any idea why this happens and how I can prevent it?

Upvotes: 1

Views: 66

Answers (1)

Filipe Silvestrim
Filipe Silvestrim

Reputation: 84

As said, it seems to be more a stage problem. Try to initialize you code via the event ADDED_TO_STAGE like the below:

package {

    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite {

        public function Main() {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            //your code should start here
        }
    }
}

Upvotes: 1

Related Questions