8 Replies Latest reply on Mar 29, 2009 8:54 AM by salut

    Cannot access a property or method of a null object reference

    salut
      I'm getting TypeError: Error #1009: Cannot access a property or method of a null object reference.

      Here's what I'm trying to do:
      1. When the user selects an item in the combobox in A.mxml (the page that contains the combobox and navigation bar), the value is passed to B.mxml for more processing.
      2. I use preinitialize="preinit()" to get the value to show up in B.mxml and creationComplete="init()" to get the value when the user is in B.mxml.
      3. The two relevant functions are getPubName and publicationSelected.
      4. tiPublicationName is the id for the TextInput component. The error message points to this block of code in publicationSelected:

      tiPublicationName.enabled = true;
      tiPublicationName.editable = true;
      tiPublicationDescription.enabled = true;
      tiPublicationDescription.editable = true;
      tiContactUserId.enabled = true;
      tiContactUserId.editable = true;
      tiPluginName.enabled = true;
      tiPluginName.editable = true;
      tiPublicationName.text = selected_pub;
      tiPublicationDescription.text = selected_pub_desc;
      tiContactUserId.text = selected_pub_userid;
      tiPluginName.text = selected_pub_pluginname;
      contentSourcesList.dataProvider = allContentSources;
        • 1. Re: Cannot access a property or method of a null object reference
          Gregory Lafrance Level 6
          I think preinitilize is too early in the app startup cycle, try initialize.

          If you still get the error, boil down your code to a simplified yet complete example that gives the error and an answer will come soon.
          • 2. Re: Cannot access a property or method of a null object reference
            salut Level 1
            Thanks for your suggestion. I tried initialize, instead of preinitialize, and it didn't work. The values passed from the custom event did not even get displayed on B.mxml. As you suggested, I'll boild down the code to the barest minimum and start afresh. Thanks.
            • 3. Re: Cannot access a property or method of a null object reference
              Level 7

              "Greg Lafrance" <webforumsuser@macromedia.com> wrote in message
              news:gqdleq$2fs$1@forums.macromedia.com...
              >I think preinitilize is too early in the app startup cycle, try initialize.
              >
              > If you still get the error, boil down your code to a simplified yet
              > complete
              > example that gives the error and an answer will come soon.

              commitProperties is likely to be even better.

              But you might also want to consider just creating public variables and
              binding your components to them.


              • 4. Re: Cannot access a property or method of a null object reference
                salut Level 1
                I'm a Flex newbie. I got the creating public variables part, like

                [Bindable] public var selected_pub:String; (in A.mxml)

                But how do I bind the components to them?

                You mean something like this?

                <mx:Label text="{selected_pub}"/> (in B.mxml)

                But how do I pass the selected_pub in A.mxml to components in B.mxml? Is there another way besides creating a custom event in an ActionScript class?

                For now, I used a custom event (e.g., in PublicationSelectedEvent.as) to pass the variables from A.mxml to B.mxml.

                Thanks for your help.
                • 5. Re: Cannot access a property or method of a null object reference
                  Level 7

                  "salut" <webforumsuser@macromedia.com> wrote in message
                  news:gqelo8$at8$1@forums.macromedia.com...
                  > I'm a Flex newbie. I got the creating public variables part, like
                  >
                  > [Bindable] public var selected_pub:String; (in A.mxml)
                  >
                  > But how do I bind the components to them?
                  >
                  > You mean something like this?
                  >
                  > <mx:Label text="{selected_pub}"/> (in B.mxml)
                  >
                  > But how do I pass the selected_pub in A.mxml to components in B.mxml? Is
                  > there
                  > another way besides creating a custom event in an ActionScript class?
                  >
                  > For now, I used a custom event (e.g., in PublicationSelectedEvent.as) to
                  > pass
                  > the variables from A.mxml to B.mxml.
                  >
                  > Thanks for your help.

                  If you think of your Application like this

                  -----------------------------
                  | Application |
                  | ^ v |
                  | A.mxml B.mxml |
                  ------------------------------

                  Then you can see that the Application is in a position to communicate with
                  both A and B. And, in fact, best practice is for A and B not to know about
                  each other.

                  So, in the Application, you need code like this:

                  private function onCreationComplete():void {
                  instanceA.addEventListener('iHaveChanged', aHasChangedHandler);
                  }

                  private function aHasChangedHandler(e:Event):void {
                  'retrieve a reference to your a instance
                  var a:A = e.currentTarget as A;
                  'get A's public property to transfer to b
                  aProp:String = a.someProperty;
                  'set B's public variable
                  instanceB.someProperty = aProp;
                  }

                  Note that no custom event was needed, but you will need code like this in
                  A.mxml:

                  private function onSomethingHappened(e:Event):void {
                  //tell parent application that it needs to change the variable on b
                  var newEvent:Event = new Event('iHaveChanged');
                  dispatchEvent(newEvent);
                  }


                  For more on this, please see
                  http://flexdiary.blogspot.com/2007/08/dispatching-events.html


                  • 6. Re: Cannot access a property or method of a null object reference
                    salut Level 1
                    Thank you so much for showing me the ropes. That's exactly what I need. So, the answer is to create an Application.mxml file that can communicate with both A.mxml and B.mxml. Thanks!
                    • 7. Re: Cannot access a property or method of a null object reference
                      Level 7

                      "salut" <webforumsuser@macromedia.com> wrote in message
                      news:gqg4gj$7j8$1@forums.macromedia.com...
                      > Thank you so much for showing me the ropes. That's exactly what I need.
                      > So, the answer is to create an Application.mxml file that can communicate
                      > with both A.mxml and B.mxml. Thanks!

                      You should already have an application.mxml file. That's the file that has
                      the same name as your project.


                      • 8. Re: Cannot access a property or method of a null object reference
                        salut Level 1
                        I've been trying to make the code your suggested work in my project. I couldn't, so I stripped it down and created a new project that contains only your code. And I still can't figure out why the addEventListener in Central.mxml is not listening to the dispatchEvent in FileA.mxml.

                        I've Central.mxml, which is the application.
                        Under the sub-folder called view are FileA.mxml and FileB.mxml.

                        Would you please look at the attached code and see what I missed?