简体   繁体   English

AS3错误代码1009-无法访问空对象引用的属性或方法

[英]AS3 Error Code 1009 - Cannot access a property or method of a null object reference

Yes, I'm sure there are millions of questions like this on SO, but this one has stumped me. 是的,我敢肯定在SO上会有数百万个这样的问题,但是这个问题让我很困惑。 Observe the code sample below: 观察下面的代码示例:

<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx"
     creationComplete="init(event)">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Bindable]
        private var data:Object;

        private function init(e:FlexEvent):void {
            this.data.item = new Array();
        }
    ]]>
</fx:Script>
</s:Group>

Why can't I create an item array on the data object? 为什么不能在data对象上创建item数组? I thought the Object class was innately declared as dynamic? 我以为Object类天生就被声明为动态的? Here is the error I get at runtime because of it: 这是我在运行时由于该错误而出现的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Could someone please help me figure out how to create my own property on this object. 有人可以帮我弄清楚如何在该对象上创建自己的属性。 Usually this is easy. 通常这很容易。 lol 大声笑

Thank you for your time. 感谢您的时间。

The issue is that data has not been instantiated and is null. 问题是data尚未实例化并且为空。

<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     xmlns:mx="library://ns.adobe.com/flex/mx"
     creationComplete="init(event)">

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Bindable]
        private var data:Object;

        private function init(e:FlexEvent):void {
            this.data = {};
            this.data.item = new Array();
        }
    ]]>
</fx:Script>
</s:Group>

Make sure data exists first. 确保数据首先存在。

private function init(e:FlexEvent):void {
   this.data = {};
   this.data.item = new Array();
}

Better still, you can do this: 更好的是,您可以执行以下操作:

private function init(e:FlexEvent):void {
    if(this.data == null) 
        this.data = {};
    this.data.item = new Array();
}

That way, whenever init is called, if data exists, it will not be overwritten with a blank object. 这样,每当调用init时,如果存在数据,则不会被空白对象覆盖。

You are confusing the data type and the data instance. 您正在混淆数据类型和数据实例。 All classes derive from object though not all objects are dynamic. 所有类都从object派生,尽管并非所有对象都是动态的。

In other words, if data is a data type that is not dynamic, then you can't set the item property. 换句话说,如果数据不是动态数据类型,则无法设置item属性。 Classes in AS3 are defined as dynamic with the dynamic keyword. AS3中的类通过dynamic关键字定义为dynamic。 Anonymous objects are dynamic by default: 默认情况下,匿名对象是动态的:

So, if you set this.data = {} then you will be able to set the item property. 因此,如果设置this.data = {}则可以设置item属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AS3-错误#1009:无法访问空对象引用的属性或方法 - AS3 - Error #1009: Cannot access a property or method of a null object reference AS3错误1009:无法访问空对象引用的属性或方法 - AS3 Error 1009 : Cannot access a property or method of a null object reference AS3-错误#1009:无法访问空对象引用的属性或方法 - AS3 - Error #1009: Cannot access a property or method of a null object reference AS3错误#1009:无法访问空对象引用的属性或方法 - AS3 Error #1009: Cannot access a property or method of a null object reference AS3-错误#1009:无法访问空对象引用的属性或方法 - AS3 -Error #1009: Cannot access a property or method of a null object reference AS3:#1009:无法访问空对象引用的属性或方法 - AS3 : #1009: Cannot access a property or method of a null object reference As3错误:错误#1009:无法访问空对象引用的属性或方法 - As3 Error: Error #1009: Cannot access a property or method of a null object reference AS3 类型错误:错误 #1009:无法访问空对象引用的属性或方法。 (按钮和 gotoAndPlay) - AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference. (button and gotoAndPlay) 错误#1009:无法访问空对象引用的属性或方法。 -AS3 - Error #1009: Cannot access a property or method of a null object reference. -AS3 AS3 TypeError:错误#1009:无法访问空对象引用的属性或方法 - AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM