5 Replies Latest reply on Jan 8, 2008 5:23 AM by m_hartnett

    XML with zero or only one entry can not be converted into an ArrayCollection - why?

    pabera
      Hey,

      I have got a Problem with XML and ArrayCollections. I get some XML Data via a HTTPRequest (e4x="false" -> so an XMLList arrives).

      I save the result in an ArrayCollection, like this...
      quote:

      [Bindable]
      private var offers:ArrayCollection = new ArrayCollection();
      private function offersRPCResult(event:ResultEvent):void {
      offers = event.result.allOffers.offer;
      }

      The structure of the XML looks like this.
      quote:

      <allOffers>
      <offer id="1" />
      <offer id="2" />
      ....
      </allOffers>

      If there is more than one <offer /> tag in the XML structure, Flex does not complain and shows the content properly. If the output looks like this..
      quote:

      <allOffers></allOffers>

      which of course can happen, I get the followed error.
      -> TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich. (Its german, because I am. But the ErrorCode #1009 should give a hint. -> Run-Time Errors

      if I have a XML output like this...
      quote:

      <allOffers>
      <offer id="1" />
      </allOffers>

      I get this error..
      -> TypeError: Error #1034: Typumwandlung fehlgeschlagen: mx.utils::ObjectProxy@5f6eca1 kann nicht in mx.collections.ArrayCollection umgewandelt werden.

      I maybe could understand the null-Object Problem, but how can I fill a DataGrid for example with just one XML entry, or even 0 entries. How can I solve that problem? I didn't try out E4X. There needs to do a lot of change in my application, too much to just try something different.

      Do you have any suggestions?
      Thx in advance
      greets, patrick
        • 2. Re: XML with zero or only one entry can not be converted into an ArrayCollection - why?
          pabera Level 1
          Well that works out fine if the output is only on object. it is actually quite simple if you have understood the framework :)

          I just got another question. I am not that familiar with that kind of programming language. If the xml result does not bring any output, flex brings up an TypeError

          TypeError: #1009 Cannot access a property or method of a null object reference.
          To get around this problem I have used a try ... catch function... like this.

          quote:

          [Bindable]
          private var offers:ArrayCollection = new ArrayCollection();
          private function offersRPCResult(event:ResultEvent):void {
          try {
          if(event.result.allOffers.offer is ArrayCollection) {
          offers = event.result.allOffers.offer as ArrayCollection;
          }
          else {
          if(offers.length == 0) {
          offers.addItem(event.result.allOffers.offer);
          }
          }
          }
          catch(e:TypeError) {
          // Do Nothing
          }
          }


          Would that be the right way to do so? Or is there a better way to solve that problem? Any additions?
          Thx anyway, you made my day..

          greets, pat
          • 3. XML with zero or only one entry can not be converted into an ArrayCollection - why?
            m_hartnett Level 3
            The try / catch block is standard and should always be used.

            You can handle the null exception a couple of ways.

            First way:
            At the beginning of this result handler you could code something like this:
            if(event.result.allOffers.offer ==null) {
            /// display a message to user .. or just do nothing
            }
            else {
            //your other processing
            }
            This would avoid the try/catch because you are checking for the error condition and reacting to it with an Alert message or disregarding it all togeather.

            The second way:
            You could let the try/catch catch the exception and then display a message to the user.

            However, I always try to avoid handling thrown Exceptions if I can.

            The try/catch is meant to catch errors you may not account for or have no control over.

            Usually checking for a null object is good programming technique.

            Lastly you should also code a fault listener for your httpservice call.
            • 4. Re: XML with zero or only one entry can not be converted into an ArrayCollection - why?
              pabera Level 1
              Thank you very much,

              but the first way you offered to handle the null Object does not work, at least in my case.
              That is what I tried before, before coming up with the try/catch solution.

              If I use the way you suggested, flex throws an TypeError no matter what.
              quote:


              [Bindable]
              private var offers:ArrayCollection = new ArrayCollection();
              private function offersRPCResult(event:ResultEvent):void {
              offers = new ArrayCollection();
              if(event.result.allOffers.offer == null) {} -> TypeError #1009 Cannot access a property or method of a null object reference.
              else {
              if(event.result.allOffers.offer is ArrayCollection) {
              offers = event.result.allOffers.offer as ArrayCollection;
              }
              else {
              if(offers.length == 0) {
              offers.addItem(event.result.allOffers.offer);
              }
              }
              offers.refresh();
              }
              }


              Thats why I used the try/catch possibility. I am just asking again, becuase I dont understand why it obviously works in your application and not in mine.

              thx anyway.. greets
              • 5. Re: XML with zero or only one entry can not be converted into an ArrayCollection - why?
                m_hartnett Level 3
                it doesnt work most likely because the object 'event.result.allOffers' is null not the event.result.allOffers.offer. It blows up on checking for the last 'offer'.

                Change the check to if(event.result.allOffers == null) {};