当我在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
也无效...
答案 0 :(得分:1)
简单地声明变量不会为对象分配任何内存,因此值为null。您必须通过调用levelThumbs
或new Array
为[]
数组实际分配内存。
public var levelThumbs:Array = new Array;
或
public var levelThumbs:Array = [];