14 Replies Latest reply on Aug 16, 2009 9:36 AM by seth.hays

    addEventListener not calling function??

    seth.hays

      I am trying to load an xml file but my loader Listener doesn't seem to be calling the object refrenced.  Any clues?

       

       

       

      Class Variable:

       

           private var xmlLoader:URLLoader;

           private var xmlRequest:URLRequest;

           private var xmlDocument:XML;

       

      public function loadXMLDocument(path:String = null):void{

           if (path == null){

                trace("XMLLoadHandler failer: null pointer");

                return;

           }

       

           this.xmlRequest = new URLRequest(path);

                trace("xmlRequest");

           this.xmlLoader = new URLLoader();

                trace(xmlLoader.toString());

           this.xmlLoader.addEventListener(Event.COMPLETE,XMLFetch);

                trace("Event Complete");

           this.xmlLoader.load(xmlRequest);

                trace("xmlLoader Complete");

      }

       

       

      private function XMLFetch(event:Event):void{

                trace("in XMLFetch");

           this.xmlDocument = new XML(event.target.data);

           this.xmlLoader.removeEventListener(Event.COMPLETE,XMLFetch)

                trace(xmlDocument.toString());

      }

       

       

       

       

      I get all the trace statements in the loadXMLDocument function but none of the trace statements in the XMLFetch function.

        • 1. Re: addEventListener not calling function??
          Virat Patel

          Hello

           

          This Code is work fine as i have check @ my side.

           

          It load all trace statement in XMLFetch.

           

          I think it might be problem in path of xml.

           

          Otherwise all code works fine

           

          let me know if you require my side code.

           

          With Regards

          Virat Patel

          • 2. Re: addEventListener not calling function??
            seth.hays Level 1

            Virat Patel wrote:

             

            Hello

             

            This Code is work fine as i have check @ my side

            It load all trace statement in XMLFetch.

            I think it might be problem in path of xml

            Otherwise all code works fine

            let me know if you require my side code.

             

            With Regards

            Virat Patel

             

            I checked over all my tomcat settings to see if there was something going wrong but I did not see anything.  My path was 127.0.0.1:8080/xmlFile/myDoc.xml however I still don't get any of the trace statements in XMLFetch.  I would be greatful if I could see what you did to get it to work.

             

            Thank you.

            • 3. Re: addEventListener not calling function??
              Virat Patel Level 2

              Hello

               

              I have just use your code in my application and cehck the URL path in different case.

               

              like URL path from localhost,hosted server and local file system.

               

              In all case it's work fine at myside.

               

              I have attaced sample code for it included all three case.

               

              Check it out.

               

               

              With Regards

              Virat Patel

              1 person found this helpful
              • 4. Re: addEventListener not calling function??
                seth.hays Level 1

                Virat Patel wrote:

                 

                I have attaced sample code for it included all three case.

                 

                Check it out.

                 

                 

                With Regards

                Virat Patel

                 

                Thanks for the help, but I don't seem to be able to download the attachment.

                • 5. Re: addEventListener not calling function??
                  seth.hays Level 1

                  I was able to finally download the example.  It works fine on my machine, however my implementation is in pure Actionscript, no mxml document.  I don't know why that would make a difference.  Everything works right up to the eventListener and then it blows up.  I will continue to dig and see if I can't figure out why this is happening.  Thanks for all your help Virat.

                  • 6. Re: addEventListener not calling function??
                    Flex harUI Adobe Employee

                    What do you mean by "it blows up"?

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: addEventListener not calling function??
                      seth.hays Level 1

                      Ok so I've made some progress.

                       

                      My error (ie. It blows up) seems to be that the addEventListener is not actually attached to the object and therefore never gets called giving me a null pointer exception when I try to actually look at the xml data.

                       

                      I found that if I call my function in my main class constructor through a new instantiation everything seems to work just fine.  However, if I try to define a class variable that happens to invoke the XMLHandler class, everything seems like it is working fine until I get to the addEventHandler.

                       

                      Maybe someone could explain why.

                       

                      Example:

                      <!-- this one works -->

                      public function MainClass(){

                           new XMLLoadHandler("http://localhost:8080/Test/my.xml");

                      }

                       

                       

                      <!-- this one doesn't work -->

                           var sc:SupportClass = new SupportClass();

                       

                           public function MainClass(){

                                // never gets here

                           }

                       

                           public class SupportClass(){

                                public function SupportClass(){

                                     new XMLLoadHandler("http://localhost:8080/Test/my.xml");

                                }

                           }

                      <!-- classes seem to be instantiated however the addEvent Listener doesn't work and xml data doesn't get loaded, returns undefined -->

                       

                       

                      <!-- this one also doesn't work -->

                           var sc:SupportClass;

                       

                           public function MainClass(){

                                sc = new SupportClass();

                                sc.loadXML();

                                // never gets here

                           }

                       

                           public class SupportClass(){

                                public function SupportClass(){ }

                                public function loadXML(){

                                     new XMLLoadHandler("http://localhost:8080/Test/my.xml");

                                }

                       

                           }

                      <!-- classes seem to be instantiated however the addEvent Listener doesn't work and xml data doesn't get loaded, returns undefined -->

                       

                       

                      • 8. Re: addEventListener not calling function??
                        Virat Patel Level 2

                        Hello

                         

                        As you have explaing that you need that thing to be done inpure actionscript.

                         

                        Can you explain more that how exactly you wanted flow for the code means your requirement.

                         

                        With Regards

                        Virat Patel

                        • 9. Re: addEventListener not calling function??
                          seth.hays Level 1

                          I built the class to load xml documents.  I figured out that the code works in the constructor, but not part as a predefined user type, my last post has examples that explain this.

                          • 10. Re: addEventListener not calling function??
                            seth.hays Level 1

                            I also found this does not work.

                             

                            public class XMLLoadHandler(){

                                 var xml:XML;

                             

                                 public function XMLLoadHandler(path:String){

                                      loadXMLDocument(path);

                                 }

                             

                                 public function loadXMLDocument(path:String = null):void{

                                      if (path == null){

                                           trace("XMLLoadHandler failer: null pointer");

                                           return;

                                      }

                                      var xmlRequest:URLRequest = new URLRequest(path);

                                      this.xmlLoader = new URLLoader();

                                      this.xmlLoader.addEventListener(Event.COMPLETE,XMLFetch);

                                      this.xmlLoader.load(xmlRequest);

                                     trace(xml.toXMLString());

                                 }

                             

                                 private function XMLFetch(event:Event):void{

                                      this.xml = new XML(event.target.data);

                                      this.xmlLoader.removeEventListener(Event.COMPLETE,XMLFetch);

                                 }

                             

                            The points of interest are in bold.

                             

                            If i trace the xml info inside XMLFetch then i get data, however if I trace it in loadXMLDocument then I get a null pointer exception.  I assume this is because the document unloads after leaving XMLFetch, but it seems to me the data should still be in the variable xml since it is declared as a class variable.  Any thoughts on this?

                            • 11. Re: addEventListener not calling function??
                              Flex harUI Adobe Employee

                              In this case, xml has not been set yet.  URLLoader is asynchronous and does not block so the next line of code after load() executes immediately but the complete event may not fire for some time after.

                               

                              Alex Harui

                              Flex SDK Developer

                              Adobe Systems Inc.

                              Blog: http://blogs.adobe.com/aharui

                              1 person found this helpful
                              • 12. Re: addEventListener not calling function??
                                seth.hays Level 1

                                I figured that was what was going on.  Is there a way to synchronize data in AS?  I would like other classes to be able to access the xmlDocument loaded here.  What would be the best way to go about waiting for the xml Document.  Thank you very much for the reply.

                                • 13. Re: addEventListener not calling function??
                                  Gregory Lafrance Level 6

                                  Because loading data in Flex is asynchronous, you often need to call some function in the loader complete handler, HTTPService result handler, etc. that starts some other process that is dependent on data being available.

                                   

                                  You could also have a bindable variable set when data is available, and have a change watcher set up so when that variable value changes reflecting the fact that data is available, other areas of the app can execute:

                                   

                                  http://www.nayansavla.com/blog/?p=43

                                   

                                  http://www.switchonthecode.com/tutorials/flex-snippet-using-changewatcher

                                   

                                  http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_7.html

                                   

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