2014-06-15 74 views
0

当我测试中的Adobe Flash CC的比赛,我得到这个错误:TypeError: Error #1009: Cannot access a property or method of a null object reference.语法错误:错误#1009:无法访问空对象引用的属性或方法

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

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

只需声明一个变量不会为该对象分配任何内存,并因此具有值null。您必须通过调用new Array[]实际为levelThumbs阵列分配内存。

public var levelThumbs:Array = new Array; 

public var levelThumbs:Array = []; 
+0

这是正确的答案,但对此的解释是错误的原因有二,所描述的过程不是的内存,但类实例的创建分配。要创建一个类的实例(这里是Array),必须分配内存。内存分配不等于创建类实例。最后也不建议在声明时创建实例。这些对象在对象自身之前将被实例化。相反,在你的构造函数中创建你的对象。 – BotMaster

相关问题