5 Replies Latest reply on May 11, 2010 1:38 AM by Graham vdR

    Help! "Cannot access a property or method of a null object reference."

    Graham vdR Level 1

      Hi there

       

      I'm relatively new to Flash, so this is probably a very basic question:

       

      I'm getting this output when I test my file:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at BillDavisWeb3_fla::MainTimeline/goScroll()

       

      It appears when I navigate away from the frame that contains the target of the goScroll function, which is as follows:

       

      function goScroll( evt ){
          if(thumbnails.x >-200 <580){
              thumbnails.x += moveAmount * moveDirection;
          }
      }

       

      I would guess that Flash is looking for the "thumbnails" movie clip in order to check its x location for this function, and can't find it since it only appears on the one frame. Does this sound right? If so, how can I stop it from looking for it on the other frames? Or have I got it all wrong?

       

      I would really appreciate any help. And please remember, I'm a noob, so the simpler the better!

       

      Thanks.

       

      (I'm using Flash CS3 on a Mac, AS3)

        • 1. Re: Help! "Cannot access a property or method of a null object reference."
          Ned Murphy Adobe Community Professional & MVP

          Your assessment sounds about right.  One thing you could do, since that function only appears to involve the thumbnails object, is to not have that function implemented/used anywhere but the frame where the thumbnail object exists.  Another option could be to test if the object is null or not before trying to target it.

          • 2. Re: Help! "Cannot access a property or method of a null object reference."
            Graham vdR Level 1

            "One thing you could do, since that function only appears to involve the  thumbnails object, is to not have that function implemented/used  anywhere but the frame where the thumbnail object exists."

             

            That's what my intention was. I haven't included it in the AS in any of the other frames. I'm not sure exactly how it works, but it seems to remember this function when I navigate away from the frame, so I don't have the error message until I go to that frame and then away. So how could I make the function redundant on the other frames?

             

            "Another option could be to test if the object is null or not before  trying to target it."

             

            Sounds good. How would I do that?

            • 3. Re: Help! "Cannot access a property or method of a null object reference."
              Graham vdR Level 1

              ... and another quick question:

               

              My limits don't seem to be working:

               

              function goScroll( evt ){
                  if(thumbnails.x >-200 <580){
                      thumbnails.x += moveAmount * moveDirection;
                  }
              }

               

              Have I used the right language / syntax here? I want the function to happen only if the x value of "thumbnails" is between -200 and 580.

               

              Thanks for the help!

              • 4. Re: Help! "Cannot access a property or method of a null object reference."
                Ned Murphy Adobe Community Professional & MVP

                For your limits you need to test each condition separately....

                 

                if(thumbnails.x > -200 && thumbnails.x < 580){...

                 

                As for the error message.  You cannot get that message in another frame unless you are calling that function in another frame.  So look to see what is calling that function in another frame and stop/remove it from doing that.

                • 5. Re: Help! "Cannot access a property or method of a null object reference."
                  Graham vdR Level 1

                  Thanks very much for your help, Ned. I realise now how difficult it is to give Flash / AS help online. There are so many factors and variables that you can't see from your end that could be affecting things.

                   

                  With a little help from a friend I managed to sort things out. I still don't know exactly why I was getting that error message, but here's my updated code:

                   

                  stage.addEventListener(Event.ENTER_FRAME, goScroll);

                   

                  function goScroll( evt ){
                      if(moveRightCheck){
                          if(thumbnails.x <690){
                              thumbnails.x += moveAmount * moveDirection;
                          }
                      }
                      else{
                          if(moveLeftCheck){
                              if(thumbnails.x >-180){
                                  thumbnails.x += moveAmount * moveDirection;
                              }
                          }
                      }
                  }

                   

                  I've created moveRight and moveLeft checks that are triggered by the same function that changes the moveDirection variable from 0 to 1 or -1 (ie mousing over my arrows). Putting the two limits in the same function meant that the same thing that stopped the clip from moving one way would also prevent it from moving back the other way.

                   

                  This seems to work perfectly.

                   

                  Thanks again.

                   

                  Graham