AS3 error #1009

∥☆過路亽.° 提交于 2019-12-13 05:34:42

问题



When I run the following code I get a error #1009 saying that the var 'list' is null?
Can someone please tell me what's wrong with this AS3 code, I've done lots of searching and read lot of info but no matter how simple the code it's still the same erro #1009 issue.
Thanks,

package  
{  
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    public class Main extends Sprite
    {    
        public var list:Array;

        public function Main() {  
            stage.addEventListener(MouseEvent.CLICK, add);
        }

        public function add(e:MouseEvent):void {
            list.push("ball");
            trace(list);
        } 
    }  
}

回答1:


It's not initialized.

public var list:Array = [];

or

public var list:Array = new Array();



回答2:


You have to instantiate the list, otherwise the list is a reference to nowhere in memory:

public var list:Array = new Array();

or (which is faster in terms of performance than the above):

public var list:Array = [];

Edit: Clarification



来源:https://stackoverflow.com/questions/5796709/as3-error-1009

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!