本文介绍了类型错误:错误#1009:无法访问空对象引用的属性或方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误,当我启动应用程序(闪存式与关闭所有,并继续),我的想法:

主要应用:

 

    

 进口events.ProductEvent;

  进口mx.collections.ArrayCollection;
  进口mx.controls.Alert;
  进口mx.events.FlexEvent;
  进口mx.rpc.events.FaultEvent;
  进口mx.rpc.events.ResultEvent;

  进口valueObject.ImageClass;
  [可绑定]
  公共变种imagesCollection:ArrayCollection的;
  保护功能xmlService_faultHandler(事件:的FaultEvent):无效
  {
    Alert.show(meeah);

  }


  保护功能xmlService_resultHandler(事件:的ResultEvent):无效
  {
    VAR imageCollection:ArrayCollection的= event.result.Images.image;
    VAR IMDATA:的imageCLASS;
    每个(VAR IM:在imageCollection对象)
    {
      IMDATA =新的imageCLASS();
      imData.url = im.url;
      imData.big_url = im.big_url;
      imData.cat = im.cat;
      imData.descript = im.descript;
      imData.price = im.price;
      imagesCollection.addItem(IMDATA);行:41

    }
    Alert.show(GG);
  }
  [可绑定]
  公共变种妈妈:ArrayCollection的=新ArrayCollection的();



  保护功能gallery1_addToCartHandler(事件:ProductEvent):无效
  {
     mama.addItem(事件);
    currentState的=购物车;
      }

    ]]≥
< / FX:脚本>
  < S:国家>
    < S:国名=正常/>
    < S:国名=购物车/>
  < / S:国家>
  < FX:声明>
    < S:的HTTPService的id =xmlService行64
                   URL =dataXml / pics.xml故障=xmlService_faultHandler(事件)
                   结果=xmlService_resultHandler(事件)/>

  < / FX:声明>

  <组件:库imagesArray ={imagesCollection}addToCart =gallery1_addToCartHandler(事件)/>

  <组件:车X =500Y =300的itemRenderer =components.CartRenderer的dataProvider ={}妈妈/>
< / S:用途>
 

画廊组成部分:

 < XML版本=1.0编码=UTF-8&GT?;
< S:组的xmlns:FX =htt​​p://ns.adobe.com/mxml/2009
         XMLNS:S =库://ns.adobe.com/flex/spark
         的xmlns:MX =库://ns.adobe.com/flex/mx>
  < FX:声明>
    <! - 将非视觉元素(例如,服务,价值的物品)在这里 - >
  < / FX:声明>

  < FX:元数据>
    [事件(名称=addToCart,键入=events.ProductEvent)
  < / FX:元数据>

  < S:布局>
    &所述氏:VerticalLayout />
  < / S:布局>

  < FX:脚本>
    <![CDATA [
      进口events.ProductEvent;

      进口mx.collections.ArrayCollection;

      进口valueObject.ImageClass;
      [可绑定]
      公共变种imagesArray:ArrayCollection的;



      公共职能goa_clickHandler(事件:MouseEvent)方法:无效
      {
        对于(VAR我:UINT = 0; I< imagesArray.length;我++)
        {
          VAR objEvent:ProductEvent =新ProductEvent(addToCart,真正的);
          objEvent.imData = imagesArray [I]
          则dispatchEvent(objEvent);
        }
      }
    ]]≥
  < / FX:脚本>
  < S:SkinnableDataContainer ID =续的dataProvider ={imagesArray}>

    < S:itemRenderer的>

      < FX:成分>

        < S:ItemRenderer的>

          &所述氏:HGroup>
            < S:标签ID =gagal文本={data.price}/>
            < MX:图片来源={data.url}WIDTH =50高度=50/>
            < S:按钮的ID =果阿标签=买入点击=outerDocument.goa_clickHandler(事件)/>
          &所述; /秒:HGroup>

        < / S:ItemRenderer的>

      < / FX:成分>



    < / S:itemRenderer的>

  &所述; /秒:SkinnableDataContainer>


< / S:组>
 

在ProductEvent类:

 包事件
{
  进口对象类型:flash.events.Event;

  进口valueObject.ImageClass;

  [可绑定]
  公共类ProductEvent扩展事件
  {

    公共变种IMDATA:的imageCLASS;


    公共职能ProductEvent(类型:字符串,气泡:布尔= TRUE)
    {
      超(类型,气泡);


    }
    重写公共职能的clone():事件
    {
      VAR eventOb:ProductEvent =新ProductEvent(类型,气泡);
      eventOb.imData = this.imData;
      返回eventOb;

    }
  }
 

本的imageCLASS:

 包的ValueObject
 {
  [可绑定]
  公共类的imageCLASS
  {
    公共变种网址:字符串;
    公共变种big_url:字符串;
    公共变种不伦不类:字符串;
    公共变种价格:字符串;
    公共变种猫​​:字符串;

    公共职能的imageCLASS()
    {
      /*this.url=url;
      this.big_url = big_url;
      this.descript =不伦不类;
      this.price =价格;
      this.cat =猫; * /
    }
  }
}
 

和XML:

 < XML版本=1.0&GT?;
<图片>
 <图像>
  <网址> poze /图片/ IMG_1163.jpg< / URL>
  < big_url> poze / big_pics / IMG_1163.jpg< / big_url>
  &其中; DESCRIPT指p&所述; / DESCRIPT>
  <价格> 99.99 LT; /价格>
  <猫> A< /猫>
 < /图片>
 <图像>
  <网址> poze /图片/ IMG_1175.jpg< / URL>
  < big_url> poze / big_pics / IMG_1175.jpg< / big_url>
  < D​​ESCRIPT> U< / DESCRIPT>
  <价格> 99.99 LT; /价格>
  <猫> A< /猫>
 < /图片>
 <图像>
  <网址> poze /图片/ IMG_1186.jpg< / URL>
  < big_url> poze / big_pics / IMG_1186.jpg< / big_url>
  &其中; DESCRIPT→1&所述; / DESCRIPT>
  <价格> 99.99 LT; /价格>
  <猫> A< /猫>
 < /图片>
 

解决方案

您正在访问 imagesCollection 变量第41行,但没有初始化它 - 它仍然包含

要么改变

  [可绑定]
公共变种imagesCollection:ArrayCollection的;
 

  [可绑定]
公共变种imagesCollection:ArrayCollection的=新ArrayCollection的();
 

或添加

  imagesCollection =新ArrayCollection的();
 

xmlService_resultHandler年初方法。

I get this error when i launch the application(flash type with dismiss all and continue) and i'm out of ideas:

The main application:

  import events.ProductEvent;

  import mx.collections.ArrayCollection;
  import mx.controls.Alert;
  import mx.events.FlexEvent;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;

  import valueObject.ImageClass;
  [Bindable]
  public  var  imagesCollection:ArrayCollection;
  protected function xmlService_faultHandler(event:FaultEvent):void
  {
    Alert.show("meeah");

  }


  protected  function xmlService_resultHandler(event:ResultEvent):void
  {
    var imageCollection:ArrayCollection=event.result.Images.image ;
    var imData:ImageClass;
    for each(var im:Object in imageCollection)
    {
      imData=new ImageClass();
      imData.url=im.url;
      imData.big_url=im.big_url;
      imData.cat=im.cat;
      imData.descript=im.descript;
      imData.price=im.price;
      imagesCollection.addItem(imData); line:41

    }
    Alert.show("gg");
  }
  [Bindable]
  public var mama:ArrayCollection=new ArrayCollection();



  protected function gallery1_addToCartHandler(event:ProductEvent):void
  {
     mama.addItem(event);
    currentState="cart";
      }

    ]]>
</fx:Script>
  <s:states>
    <s:State name="normal"/>
    <s:State name="cart"/>
  </s:states>
  <fx:Declarations>
    <s:HTTPService id="xmlService"  line 64
                   url="dataXml/pics.xml" fault="xmlService_faultHandler(event)"
                   result="xmlService_resultHandler(event)"/>

  </fx:Declarations>

  <components:Gallery imagesArray="{imagesCollection}" addToCart="gallery1_addToCartHandler(event)"   />

  <components:cart  x="500" y="300" itemRenderer="components.CartRenderer" dataProvider="{mama}"  />
</s:Application>

The Gallery component:

<?xml version="1.0" encoding="utf-8"?>
<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"   >
  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>

  <fx:Metadata>
    [Event(name="addToCart",type="events.ProductEvent")]
  </fx:Metadata>

  <s:layout>
    <s:VerticalLayout/>
  </s:layout>

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

      import mx.collections.ArrayCollection;

      import valueObject.ImageClass;
      [Bindable]
      public var imagesArray:ArrayCollection;



      public function goa_clickHandler(event:MouseEvent):void
      {
        for (var i:uint=0; i<imagesArray.length;i++)
        {
          var objEvent:ProductEvent=new ProductEvent("addToCart",true);
          objEvent.imData=imagesArray[i] ;
          dispatchEvent(objEvent);
        }
      }
    ]]>
  </fx:Script>
  <s:SkinnableDataContainer id="cont"  dataProvider="{imagesArray}">

    <s:itemRenderer >

      <fx:Component>

        <s:ItemRenderer>

          <s:HGroup>
            <s:Label id="gagal" text="{data.price}"/>
            <mx:Image source="{data.url}" width="50" height="50" />
            <s:Button id="goa"  label="buy" click="outerDocument.goa_clickHandler(event)"/>
          </s:HGroup>

        </s:ItemRenderer>

      </fx:Component>



    </s:itemRenderer>

  </s:SkinnableDataContainer>


</s:Group>

The ProductEvent class:

package events
{
  import flash.events.Event;

  import valueObject.ImageClass;

  [Bindable]
  public class ProductEvent extends Event
  {

    public var imData:ImageClass;


    public function ProductEvent(type:String,bubbles:Boolean=true)
    {
      super(type,bubbles);


    }
    override public function clone():Event
    {
      var eventOb:ProductEvent=new ProductEvent(type,bubbles);
      eventOb.imData=this.imData;
      return eventOb;

    }
  }

The ImageClass:

package valueObject
 {
  [Bindable]
  public class ImageClass
  {
    public var url:String;
    public var big_url:String;
    public var descript:String;
    public var price:String;
    public var cat:String;

    public function ImageClass()
    {
      /*this.url=url;
      this.big_url=big_url;
      this.descript=descript;
      this.price=price;
      this.cat=cat;*/
    }
  }
}

and the xml:

<?xml version="1.0"?>
<Images>
 <image>
  <url>poze/pics/IMG_1163.jpg</url>
  <big_url>poze/big_pics/IMG_1163.jpg </big_url>
  <descript>P</descript>
  <price>99.99</price>
  <cat>A</cat>
 </image>
 <image>
  <url>poze/pics/IMG_1175.jpg</url>
  <big_url>poze/big_pics/IMG_1175.jpg</big_url>
  <descript>U</descript>
  <price>99.99</price>
  <cat>A</cat>
 </image>
 <image>
  <url>poze/pics/IMG_1186.jpg</url>
  <big_url>poze/big_pics/IMG_1186.jpg</big_url>
  <descript>L</descript>
  <price>99.99</price>
  <cat>A</cat>
 </image>
解决方案

You're accessing imagesCollection variable in line 41, but haven't initialized it - it still contains null

Either change

[Bindable]
public  var  imagesCollection:ArrayCollection;

To

[Bindable]
public  var  imagesCollection:ArrayCollection = new ArrayCollection();

or add

imagesCollection  = new ArrayCollection();

to the beginning of xmlService_resultHandler method.

这篇关于类型错误:错误#1009:无法访问空对象引用的属性或方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:39