3 Replies Latest reply on Jul 16, 2009 6:38 AM by jbenson@oper8

    TypeError: Error #2007: Parameter listener must be non-null.

    slapyo

      I've searched the forums and all over the web and have not found an answer.  I don't know why I get this error.  It's extremely simple at the moment, starting the app.  It was working when I had all the functions not in an object.  Any help would be greately appreciated.

       

      TypeError: Error #2007: Parameter listener must be non-null.
          at flash.events::EventDispatcher/removeEventListener()

       

      $(document).ready(function() {
          MyApp.init();
      });

       

      var MyApp = {
          prefsFile: '',
          prefsXML: '',
          stream: '',
         
          init: function() {
              window.nativeWindow.addEventListener(air.Event.CLOSING, this.windowClosingHandler);
              this.prefsFile = air.File.applicationStorageDirectory;
              this.prefsFile = this.prefsFile.resolvePath("preferences.xml");
              this.readXML();
          },
         
          readXML: function() {
              this.stream = new air.FileStream();
             
              if (this.prefsFile.exists) {
                  this.stream.open(this.prefsFile, air.FileMode.READ);
                  this.processXMLData();
              } else {
                  this.saveData();
              }
          },
         
          processXMLData: function() {
              this.prefsXML = this.stream.readUTFBytes(this.stream.bytesAvailable);
              this.stream.close();
              var domParser = new DOMParser();
              this.prefsXML = domParser.parseFromString(this.prefsXML, "text/xml");
              var windowState = this.prefsXML.getElementsByTagName("windowState")[0];
              window.moveTo(windowState.getAttribute("x"), windowState.getAttribute("y"));
          },
         
          saveData: function() {
              this.createXMLData();
              this.writeXMLData();
          },
         
          createXMLData: function() {
              var cr = air.File.lineEnding;
                 
              this.prefsXML =   "<?xml version='1.0' encoding='utf-8'?>" + cr
                  + "<preferences>" + cr
                  + "    <windowState" + cr
                  + "        x = '" + window.screenLeft.toString() + "'" + cr
                  + "        y = '" + window.screenTop.toString() + "'" + "/>" + cr
                  + "    <saveDate>" + new Date().toString() + "</saveDate>" + cr
                  + "</preferences>";
          },
         
          writeXMLData: function() {
              this.stream = new air.FileStream();
              this.stream.open(this.prefsFile, air.FileMode.WRITE);
              this.stream.writeUTFBytes(this.prefsXML);
              this.stream.close();
          },
         
          windowClosingHandler: function(e) {
              e.preventDefault();
              window.nativeWindow.removeEventListener(air.Event.CLOSING, this.windowClosingHandler);
              this.saveData();
              air.NativeApplication.nativeApplication.exit();
          }
      };

        • 1. Re: TypeError: Error #2007: Parameter listener must be non-null.
          jbenson@oper8 Level 2

          within your function "MyApp.windowClosingHandler" you call removeEventListener like so:

           

          window.nativeWindow.removeEventListener(air.Event.CLOSING, this.windowClosingHandler);

           

          The issue is that your function (windowClosingHandler) is being called by the event air.Event.CLOSING and windowClosingHandler is not a member of it.  (IE this in that context is not the function/namespace "MyApp" but rather the event)

           

          So change your code from:

          window.nativeWindow.removeEventListener(air.Event.CLOSING, this.windowClosingHandler);

          To

          window.nativeWindow.removeEventListener(air.Event.CLOSING, MyApp.windowClosingHandler);

           

          and you will no longer receive that message.

           

          hope that helps!

          • 2. Re: TypeError: Error #2007: Parameter listener must be non-null.
            slapyo Level 1

            Actually, that didn't work.  I forgot to mention that I tried that.  This is the solution that I found.

             

            Function.prototype.bind = function(context) {
                var fn = this;
                return function(){
                    if(arguments.length !== 0) {
                        return fn.apply(context, arguments);
                    } else {
                        return fn.call(context);
                    }
                };
            };

             

            Then inside in the init function and windowClosingHandler I used this.

             

                init: function() {
                    window.nativeWindow.addEventListener(air.Event.CLOSING, Stoker.windowClosingHandler.bind(Stoker));
                    this.prefsFile = air.File.applicationStorageDirectory;
                    this.prefsFile = this.prefsFile.resolvePath("preferences.xml");
                    this.readXML();
                },

             

                windowClosingHandler: function(e) {
                    e.preventDefault();
                    window.nativeWindow.removeEventListener(air.Event.CLOSING, Stoker.windowClosingHandler.bind(Stoker));
                    this.saveData();
                    air.NativeApplication.nativeApplication.exit();
                }

            • 3. Re: TypeError: Error #2007: Parameter listener must be non-null.
              jbenson@oper8 Level 2

              That's interesting.  When I tested your code the only error I raised on application exit after changing to MyApp was an error on the method saveData.

               

              Regardless glad you got it working!