Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

apache flex - TypeError: Error #1009: Cannot access a property or method of a null object reference

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

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Magazin/xmlService_resultHandler()[D:Documents and SettingschechuAdobe Flash Builder 4MagazinsrcMagazin.mxml:41] at Magazin/__xmlService_result()[D:Documents and SettingschechuAdobe Flash Builder 4MagazinsrcMagazin.mxml:64] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:dev4.xframeworksprojects pcsrcmx pchttpHTTPService.as:989] at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:dev4.xframeworksprojects pcsrcmx pcAbstractInvoker.as:318] at mx.rpc::Responder/result()[E:dev4.xframeworksprojects pcsrcmx pcResponder.as:56] at mx.rpc::AsyncRequest/acknowledge()[E:dev4.xframeworksprojects pcsrcmx pcAsyncRequest.as:84] at DirectHTTPMessageResponder/completeHandler()[E:dev4.xframeworksprojects pcsrcmxmessagingchannelsDirectHTTPChannel.as:446] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at flash.net::URLLoader/onComplete()

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...