Skip navigation
Currently Being Moderated

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Apr 5, 2012 8:41 PM

Tags: #problem #air #cs5 #error #flash #script #text #as3 #cs5.5 #3.0 #swf #button #xml #action #3 #code_snippets #action_script_3 #event #movieclip #ac3 #game #actionscript3 #function #actionscript_3.0

I am currently making a media player and everything was working fine before i put this code in the the first scene.

 

btn_next.addEventListener(MouseEvent.CLICK, nextsong);
function nextsong(event:MouseEvent):void
{
          lastposition = 0;
          soundchannel.stop();
          btn.btn_pause.visible = false;
          trace("finished");
          isPlaying=false;

          gotoAndPlay(1,"Airy");
}

After you press the button it is supposed to jump to the scene "Airy" but i get this error message:

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at player1_fla::MainTimeline/frame2()
          at flash.display::Sprite/constructChildren()
          at flash.display::Sprite()
          at flash.display::MovieClip()
          at flash.display::MovieClip/gotoAndPlay()
          at player1_fla::MainTimeline/nextsong()

This error occurs after the button is pressed and the next scene begins playing. Im fairly new to action script 3.0 and im not sure what is wrong. When i run the debugger it points out this line of script:

 

airybtn.addEventListener(MouseEvent.CLICK, playsound1);

the function playsound1 is:

 

function playsound1(event:MouseEvent):void
{
          if (! isPlaying1)
          {
                    soundchannel1 = mysound1.play(lastposition,0);
                    airybtn.btn_pause.visible = true;
                    isPlaying1 = true;

          }
          else
          {
                    lastposition = soundchannel1.position;
                    soundchannel1.stop();
                    airybtn.btn_pause.visible = false;
                    trace(lastposition.toFixed(0), mysound1.length.toFixed(0));
                    isPlaying1 = false;
          }

          soundchannel1.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

          function onPlaybackComplete(event:Event):void
          {
                    lastposition = 0;
                    soundchannel1 = mysound1.play(0,0);
                    soundchannel1.stop();
                    airybtn.btn_pause.visible = false;
                    trace("finished");
                    isPlaying1=false

          }
}

PLease help me....

 
Replies
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 5, 2012 10:09 PM   in reply to HibernateZz

    click file > publish settings > flash > tick "permit debugging" > ok.  retest.

     

    the problematic line number will be in the error message allowing you to pinpoint the object that doesn't exist when your code executes.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 6, 2012 4:13 AM   in reply to HibernateZz

    Error messages are ordered such that the "at" lines are ordered closest to furthest from the problem, so you want to look at line 58 of frame 2 to find the problem.  If you are using scenes and only have one frame in scene 1, then frame 2 would be the first frame in scene 2

     

    If line 58 is the line you say was pointed to by the debugger, then chances are airybtn does not exist as far as your code sees it.

     

    The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....

     

    - is declared but not instantiated

    - doesn't have an instance name (or the instance name is mispelled)

    - does not exist in the frame where that code is trying to talk to it

    - is animated into place but is not assigned instance names in every keyframe for it

    - is one of two or more consecutive keyframes of the same objects with no name (or a different name) assigned in the preceding frame(s).

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 6, 2012 6:28 AM   in reply to HibernateZz

    what's line 58 in frame 2?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 6, 2012 6:45 AM   in reply to HibernateZz

    I'll let kGlad continue with trying to help, but to answer your question...

     

    var someMC:Movieclip;         // declared but not instantiated

     

    var someMC:MovieClip = new MovieClip();     // declared and instantiated

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 6, 2012 6:57 AM   in reply to Ned Murphy

    do you have code in more than one layer of frame 2? 

     

    if yes, put all your code in one layer and retest and copy-paste the problematic line of code.  if no, take a screen shot showing your timeline at frame 2 with all its layers and showing the actions panel with all its code for that frame and the layer with your code.

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 6, 2012 8:01 AM   in reply to HibernateZz

    put all your code on the same layer so you can get an accurate line number in your error message without doing arithmetic.

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 6, 2012 10:26 PM   in reply to HibernateZz

    attach a screen shot showing your timeline at frame 2 with all its layers showing and include the actions panel with all the code for frame2.

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 7, 2012 6:37 AM   in reply to HibernateZz

    attach a screen shot showing your timeline at frame 2 with all its layers showing (which you did) and include (in the screenshot) the actions panel with all the code for frame2 (which you didn't do).

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 7, 2012 7:43 AM   in reply to HibernateZz

    just include the first 40 lines or so.

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 7, 2012 11:05 PM   in reply to HibernateZz

    no.  i can't see your line numbers and you're misreporting what's on the problematic line.

     
    |
    Mark as:
  • kglad
    63,097 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 8, 2012 6:45 AM   in reply to HibernateZz

    ok.  use my website to send an initial email.  in a follow-up, you'll attach an image:  www.kglad.com

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 10, 2012 2:26 PM   in reply to Ned Murphy

    Thanks for the plain English replay Ned! With my code, the error was as you mentioned:

    - is one of two or more consecutive keyframes of the same objects with no name (or a different name) assigned in the preceding frame(s).

     

    I had a button which was on two keyframes with different names. It would be really nice in the next version of Flash if Adobe could make some plain English error codes though!

     

    Dan

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 10, 2012 3:47 PM   in reply to Daniel Gimness

    You're welcome Dan.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2012 5:08 PM   in reply to Ned Murphy

    I am having a similar problem.

     

    Please excuse me if this is a stupid question, but I'm still new to flash/as3.

     

    I've modified a small game from the book I've been studing, and it works great.

     

    The player object was controlled by it's own class, and sitting directly on the stage.

     

    In the player class, it adds the eventlisteners for the keyboard to the stage (stage.addEventListener....).

     

    So, I changed it so this was level 1 in a bigger game.

     

    I moved all symbols and objects from the stage of my FLA into a movie class symbol I called Level01, which used the class Level01, which had the same code as the main class had before. I then created a new "main" class that adds the Level01 symbol to the stage when I press a start button.

     

    This all works fine, but my player class now gives me an error (like above) when calling the stage.addEventListener...

     

    I have tried eliminating stage, replacing it with this, parent, and a combination of those, but it doesn't work.

     

    What am I missing?

     

    If you need the source code, please let me know.

     

    Thanks,

     

    Ole

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2012 6:08 PM   in reply to KING-OLE

    You should show the code that is relevant to your problem.  If you are trying to target something when initially instantiating the object, it is possible you need to wait for it to be ADDED_TO_STAGE (an event) before attempting such targeting.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2012 9:37 PM   in reply to Ned Murphy

    Thanks Ned.

     

    That helped, but opened up for a new problem.

     

    My player won't jump anymore, so I need to do some debugging to figure out what's going on.

     

    Thanks for your help though.

     

    Ole

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 30, 2012 4:24 AM   in reply to KING-OLE

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points