【问题标题】:SyntaxError: Error #1009: Cannot access a property or method of a null object referenceSyntaxError:错误 #1009:无法访问空对象引用的属性或方法
【发布时间】:2014-08-05 14:38:30
【问题描述】:

当我在 Adob​​e 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 也不起作用...

【问题讨论】:

    标签: actionscript-3 flash actionscript movieclip


    【解决方案1】:

    仅仅声明一个变量不会为对象分配任何内存,因此其值为 null。您必须通过调用new Array[]levelThumbs 数组实际分配内存。

    public var levelThumbs:Array = new Array;
    

    public var levelThumbs:Array = [];
    

    【讨论】:

    • 这是正确的答案,但解释是错误的,原因有两个,描述的过程不是内存分配而是类实例的创建。要创建类(此处为数组)的实例,必须分配内存。内存分配不等于创建类实例。最后也不推荐在声明时创建实例。这些对象将在对象本身之前被实例化。而是在构造函数中创建对象。
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多