Search code examples
actionscript-3flashactionscriptmovieclip

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


When I test my game in Adobe Flash CC, I get this error: TypeError: Error #1009: Cannot access a property or method of a null object reference.

This is basically the code around the error (I removed the unimportant parts to make it clearer):

package ui.levelSelect {
    import flash.display.MovieClip;

    public class LevelsContainer extends MovieClip {

        public var levelThumbs:Array;
        public var levels:Array = [{name:'level1'},{name:'level2'}];

        public function LevelsContainer(){

            for(var i:String in levels) {
                var index:int = int(index);

                levelThumbs[index] = new MovieClip; //This is the line where I get the error

            }

        }



    }

}

What causes this error? levelThumbs is already declared right? Changing it to this.levelThumbs doesn't work either...


Solution

  • Simply declaring a variable does not allocate any memory for the object, and thus have the value null. You have to actually allocate memory for levelThumbs array by calling new Array or [].

    public var levelThumbs:Array = new Array;
    

    or

    public var levelThumbs:Array = [];