5 Replies Latest reply on Oct 7, 2009 7:35 AM by Alex-SM

    Error when reading data in to ArrayCollection using HTTPService

    Alex-SM Level 1

      Hi,

      I am trying to read data in to ArrayCollection using HTTPService result property:


      [Bindable] private var myData:ArrayCollection=new ArrayCollection();


         private function result_handler(e:ResultEvent):void
         {
              myData = e.result.root.contact;
         }

        <mx:HTTPService id="post_submit"
             url="test.php"
             method="POST"
             result="result_handler(event)"
             fault="fault_handler(event)">
             <mx:request xmlns="">
                ...
             </mx:request>
         </mx:HTTPService>

       

      I am getting the following error when there is no "contact" record in the xml root:

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

       

      Also I am getting the following error when there is only ONE  "contact" record in the xml root:

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@280c9ca1 to mx.collections.ArrayCollection.

       

      And it works fine when there is two or more "contact" records in the xml root.

       

      Am I missing something?

       

      Thanks

        • 1. Re: Error when reading data in to ArrayCollection using HTTPService
          Gregory Lafrance Level 6

          This might work:

           

          private function result_handler(e:ResultEvent):void{
              if(e.result.root.contact != null){

                  myData = e.result.root.contact as ArrayCollection;

              }  

          }

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: Error when reading data in to ArrayCollection using HTTPService
            Madhav Subedi Level 4

            php part:

             

             

            $xml = '<?xml version="1.0" encoding="utf-8"?>';
            $xml .= "<root>";
            $xml .= "<contacts>";
            
            $query = "select * from contact";
            $rs = query($query);
            
            if(numrows > 0)
            {
                 while($row = next_record )
                 {
                      $xml .= "<contact>";
                      .
                      .
                      .
                      .
                      .
                      $xml .= "</contact>";
                 }
            }
            $xml .= "</contacts>";
            $xml .= "<count>".numrows."</count>";
            $xml .= "</root>";
            echo $xml;
            

             

             

            flex part:

             

             

            private function result_handler(e:ResultEvent):void
            {
                 if(e.result.root.count == 1)
                      myData = new ArrayCollection([e.result.root.contacts.contact]);
                 else if(e.result.root.count > 1)
                      myData = e.result.root.contacts.contact;
                 else
                 {
                      myData = new ArrayCollection();
                 }
            }
            
            • 3. Re: Error when reading data in to ArrayCollection using HTTPService
              UbuntuPenguin Level 4

              Did you specify the result format ? If it is e4x , you can just do (e as XML) and proceed from there

              • 4. Re: Error when reading data in to ArrayCollection using HTTPService
                Matt Le Fevre Level 4

                I am getting the following error when there is no "contact" record in the xml root:

                 

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

                 

                 

                     You'll be getting this error because it's trying to access nodes that don't exist;

                 

                     there are no 'contact' records, thus when it tries to access node 'root.contact', it doesn't exist, hence it's receiving NULL when it's expecting      something

                 

                Also I am getting the following error when there is only ONE  "contact" record in the xml root:

                TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@280c9ca1 to mx.collections.ArrayCollection.

                 

                And it works fine when there is two or more "contact" records in the xml root.

                 

                Am I missing something?

                 

                Thanks

                 

                      When there is only 1 value, it'll be returned as an object (Arrays are used for multiple items). So it throws an error as it isn't expecting a single           object, it wants an Array.

                 

                     This is also why when it returns 2+ records, there is no error. Multiple results will be returned as an Array, what your flex app was expecting.

                 

                 

                     My initial guess to solve the first problem would be similar to Greg's, although i'm guessing you'd want;

                 

                    if(e.result != null){

                 

                     instead of


                     if(e.result.root.contact != null){

                 

                    as you'd initially want to check if anything was returned in the result, rather than checking to see if a certain node exists.

                 

                     If you attempt to see if a certain node isn't null, and the node doesn't exist, it'll probably throw another #1009

                 

                 

                     For the #1034 error, you'll have to check to see how many results were returned, as Madhav has described for you.