12 Replies Latest reply on May 6, 2009 10:03 AM by kid electric

    Local XML call gives Error #1009: Cannot access a null object

    kid electric Level 1

      Kind of baffled as to what is going on...

       

      I am calling a function "loadDownloads" which is set to load a local "downloads.xml" file (located in the same src folder as the mxml application); this XML is very simple, containing a "downloads" root with multiple "download" items within it (each is a short description and url)

       

      I have set my compiler settings for -use-network="false" to be able to load the XML locally, and I use an HTTPService.send() to trigger the load.  My HTTPService is defined as follows:

       

       

      <mx: HTTPService id="httpService" resultFormat="e4x" 
       fault="httpService_fault(event)" result="httpService_result(event)" />
      

      I have set the fault event handler so that, in case of a fault, an alert shows the error information; the result handler simply does the following:

       

       

      private function httpService_result(evt:ResultEvent):void {
           xmlContent = XML(evt.result);}
      

       

      Yes, all variables were previously declared and typed.  This does not throw any errors (it did at first, when I had incorrect filenames for the URL and when I had malformed XML statements, so I know the fault handler is working) for accessing the XML file

       

      However, when I try to access the items/nodes in the XML, I get this runtime Error #1009: Cannot access method or property of null object, and sure enough, xmlContent is showing as null.  Here is how I am trying to access them:

       

       

      for each (xmlDownload in xmlContent.downloads) {
           // do stuff here like...
           linkLabel.text = xmlDownload.@label.toString();
      }
      

       

       

      Any any any ideas why HTTPService is seeing the XML file and says it's okay, but does not seem to fetch the XML into the xmlContent:XML object?

        • 1. Re: Local XML call gives Error #1009: Cannot access a null object
          ATIF FAROOQ Level 3

          hi,

          there are couple of things you do to debug it. first check whether the

          result is coming in the result handler by tracing it or adding breakpoint in

          your resultHandler and also trace fault handler that its getting any fault

          or not.Also check whether your xml is correctly formatted or not.

          • 2. Re: Local XML call gives Error #1009: Cannot access a null object
            kid electric Level 1

            thanks for the suggestions... unfortunately I have tried these techniques and the fault handler IS getting feedback, because it was popping alerts when the url/filename was incorrect (ie. file could not be found) and when the XML was not correctly formatted.  Once I fixed those, the alerts went away.

             

            I tried using some Trace statements in the Result event, but the code breaks and throws error 1009 before anything gets traced?  I'll try some more traces in the meantime to see if I can step through and figure out exactly when the problem is happening where there is a call to the null XML...

            • 3. Re: Local XML call gives Error #1009: Cannot access a null object
              m_hartnett Level 3

              If this code is getting into your loop then the problem might be in the toString() method or the @label attribute.

               

              Before you go into the loop, try an Alert with xmlContent.toXmlString() and see if what the XML looks like.

              1 person found this helpful
              • 4. Re: Local XML call gives Error #1009: Cannot access a null object
                Gregory Lafrance Level 6

                You should be able to comment out the other lines in the result handler and just trace(event.result).

                 

                If you can't even trace that, then perhaps the file is not being loaded.

                 

                You might try loading an XMLList object in the result handler:

                 

                var xl:XMLList = event.result.downloads as XMLList;

                1 person found this helpful
                • 5. Re: Local XML call gives Error #1009: Cannot access a null object
                  kid electric Level 1

                  Both helpful suggestions... I will try these and hop back on here if this doesn't reveal anything new

                  • 6. Re: Local XML call gives Error #1009: Cannot access a null object
                    kid electric Level 1

                    Okay, so... an interesting development in my debugging of this problem: it appears that, due to some sort of latency with the HTTPService request, my code is continuing forward and trying to access the XML file BEFORE it has had a chance to load.  The reason I assume this is because of this:

                     

                    on click of the Downloads button, I call loadDownloads():

                     

                     

                    private function loadDownloads():void {
                         httpService.url="downloads.xml";
                         httpService.send();
                         var xmlDownloads:XMLList = xmlContent.downloads.download;
                         for each (xmlDownload in Downloads) {
                         // do a bunch of stuff here, which it doesn't even get to, because it breaks at for-each...
                         }
                    }
                    

                    Now, the xmlContent is created in the httpService_result function I have defined, including adding an Alert to pop up and show me the XML loaded:

                     

                     

                    private function httpService_result(evt:ResultEvent):void {
                         var sTitle:String = "Result:";
                         xmlContent = XML(evt.result);
                         var sText:String = xmlContent.toString();
                         alert = Alert.show(sText, sTitle);
                    }
                    

                     

                    Now, here's what happens when I run the app:  if I click on the Downloads button, the FIRST thing it does is pop up Error #1009: Cannot access null object (on line 172, which is the for...each statement); if I click "Continue", THEN it shows the popup Alert containing the XML, which tells me that -- at least when the Alert shows -- the XML object is NOT null.

                     

                    Logically, I am calling creation/load of the XML object before making a call to it; however, it seems the httpService takes some time and the program continues to the next lines of code in the interim, therefore the httpService_result does not occur fast enough to populate XML before I make a call of it?

                     

                    Is this actually what's happening?  If so, how can I force the program to wait until the httpService.send has successfully completed and has a result before continuing forward to the next instructions?

                    • 7. Re: Local XML call gives Error #1009: Cannot access a null object
                      Gregory Lafrance Level 6

                      The answer to your question is that Flex is asynchronous, so you can't assume data requests with HTTPService have completed and try to access the data.

                       

                      Your code should be in the HTTPService result handler:

                       

                      var xmlDownloads:XMLList = xmlContent.downloads.download;
                           for each (xmlDownload in Downloads) {

                       

                       

                      Or else you can call a function in the HTTPService that does this.

                      • 8. Re: Local XML call gives Error #1009: Cannot access a null object
                        kid electric Level 1

                        Did some forum searching, and yes, it looks like I'm right that the lag in HTTPService means that I am trying to access an object before it has had a chance to be populated by the httpService.send():

                         

                        http://forums.adobe.com/message/17375#17375

                         

                        However, I am still a little confused by the recommendation made in that thread: it says to put the logic in the event handler.  Does this mean I should put all of my code (like the for...each) into the httpService_result function?

                         

                        Since my HTTPService will be loading DIFFERENT content (3 different XML files: one for downloads, one for services offered, and one for contact info), does this mean I should use a Switch inside the httpService_result to set the different code for each different type of XML?  Or would it be better to simply create 3 different HTTPService objects in MXML at initialization, and have 3 different _result handlers accordingly (one for each httpService)?

                         

                        I think I'm getting close to the answer.. this is my first time using HTTPService (I've done an AIR app, but the local file access is a bit different), so any advice in this area would be appreciated.  In the meantime, I'll try one of the two ideas I just mentioned.  I solve my own problem, I'll post the solution here.

                        • 9. Re: Local XML call gives Error #1009: Cannot access a null object
                          kid electric Level 1

                          I understand it is asynchronous, and that's the problem... however, it seems like such difficulty is easier to handle in, say, AJAX, because you can check for completion codes to ensure you have retrieved data before trying to access it.  Is there any such method in Flex (doesn't look like it)

                           

                          Since you say I should put all work in the _result event handler, does it make sense to simply have a different httpService object for each XML I plan on calling, since the results will be handled differently?

                          • 10. Re: Local XML call gives Error #1009: Cannot access a null object
                            Gregory Lafrance Level 6

                            I would have 3 HTTPService and handle each in its own result handler.

                            • 11. Re: Local XML call gives Error #1009: Cannot access a null object
                              m_hartnett Level 3

                              Three service objects would be better.

                              Also remember that you cannot call one request right after another because you can have only active request at a time.

                               

                              You should call each new request out of the resultHandler of the completing request.

                              • 12. Re: Local XML call gives Error #1009: Cannot access a null object
                                kid electric Level 1

                                Thanks, everyone.  That makes sense.

                                 

                                Not worried about the requests being called simultaneously, though out of curiosity... is there a way to check whether an httpService request is available before calling another one?  What happens if you attempt to do it while one is still running?  Does it give an error, or simply not execute?