SyntaxError:错误#1009:无法访问空对象引用的属性或方法

时间:2014-06-15 17:43:10

标签: actionscript-3 flash actionscript movieclip

当我在Adobe Flash CC中测试游戏时,出现此错误:TypeError: Error #1009: Cannot access a property or method of a null object reference.

这基本上是围绕错误的代码(我删除了不重要的部分以使其更清晰):

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

            }

        }



    }

}

导致此错误的原因是什么? levelThumbs已被宣布为正确?将其更改为this.levelThumbs也无效...

1 个答案:

答案 0 :(得分:1)

简单地声明变量不会为对象分配任何内存,因此值为null。您必须通过调用levelThumbsnew Array[]数组实际分配内存。

public var levelThumbs:Array = new Array;

public var levelThumbs:Array = [];