Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

why o why, TypeError: Error #1009: Cannot access a property or method of a null object reference?

Participant ,
Mar 13, 2015 Mar 13, 2015

So I have this scoreboard script  which is basically gives points for clicking on the correct button and subtract points if clicking on the wrong button. Make story short, the script, which run from actionscript.as file, works great as long it's on the first keyframe sequence. but when trying to run it on the next keyframe sequence it gives me this Error #1009: error message (full message bellow). Please look at my script bellow and tell me what's wrong.

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

  at MethodInfo-3()

  at scoreMe2()

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

  at scoreMe2/frame1()

My Script:

package {

  import flash.display.MovieClip

  import flash.events.MouseEvent;

  public class scoreMe2 extends MovieClip {

  public function scoreMe2() {

  //Scoreboard starts here:

  var score: uint;

  function init(): void {

  score = 100;

  scorecounter.text = "SCORE:" + score.toString();

  clip.buttonMode = true;

  clip.addEventListener(MouseEvent.CLICK, on_press1);

  gButton.buttonMode = true;

  gButton.addEventListener(MouseEvent.CLICK, on_press2);

  }

  //Positive button adding points

  function on_press1(event: MouseEvent): void {

  updateScore();

  }

  function updateScore(): void {

  gotoAndStop(2);

  score += 100;

  scorecounter.text = "SCORE:" + score.toString()

  stop();

  }

  //Negative button subtracting points

  function on_press2(event: MouseEvent): void {

  score += -20;

  scorecounter.text = "SCORE:" + score.toString();

  }

  init();

  //Scoreboard ends here:

  }

  }

}

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2015 Mar 14, 2015

you're trying to reference some object that doesn't exist when your code executes.

to pinpoint the error click file>publish settings>'permit debugging'.  retest.

the problematic line number will be in the error message allowing you to pinpoint the problem.

and, in general, you might want to book mark:

Debugging ActionScript 3.0 Errors, http://forums.adobe.com/docs/DOC-2542;

Debugging AcriptScript That Triggers No Errors,     http://forums.adobe.com/docs/DOC-2572;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 14, 2015 Mar 14, 2015

I already pinpointed the problem. The script is executing on the first frame when the buttons are only later on. How do I tell the script to execute at the right time? Generally it suppose to be a scoreboard that should run throughout the game. If i'm on the wrong track here please let me know. what do I have to do to run a scoreboard throughout the game?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2015 Mar 14, 2015

you can put your references in a function and call that function when the frame that contains them plays.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2015 Mar 15, 2015

I kinda understand what you are saying but I don't know how to go about it. Would you please  give me an example?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2015 Mar 15, 2015
LATEST

// in frame 1

function addListenersF():void{

//your button/movieclip references here

}

// in the first keyframe where your button/movieclip exist:

addListenersF();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines