Skip to main content
sergey_landar
Inspiring
April 6, 2010
Answered

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

  • April 6, 2010
  • 2 replies
  • 647 views

I have .fla file with 2 keyframes:

1 its screen where player plaing..

2 its screen where player see message like 'you are winner'

Also i separated code from .fla file.

So i can not clear why i see this #1009 error after i go to label 'p2'.

Can anyone explaine why its happend and how i can solve it?

attached image for help.

code:

package {

     import flash.events.*;
     import flash.display.*;
     import flash.text.TextField;

     public class GuessNumber extends MovieClip {
          var guessNumber:Number;
          var points:Number = 0;    
          var level:Number = 1;
          var lastGuessNumber:Number = 5;
         
          //constructor
          public function GuessNumber() {
               pointsView.text = "0";
               levelView.text = "1";
               addEventListener(Event.ADDED_TO_STAGE,startGuess);
          }
         
          private function startGuess(e:Event=null):void {
               guessBtn.useHandCursor=true;
               guessBtn.enabled=true;
               playAgainBtn.useHandCursor=true;
               playAgainBtn.enabled=false;
               levelNumbers.text="betwen 1 and " +  lastGuessNumber;
               yourGuessText.text = "";
               levelView.text = String(level);
               pointsView.text = String(points);
              
               guessNumber=Math.ceil(Math.random()*lastGuessNumber);
               trace(guessNumber);
              
               removeEventListener(Event.ADDED_TO_STAGE,startGuess);
               guessBtn.addEventListener(MouseEvent.CLICK,checkingYourGuess);
          }
         
          private function checkingYourGuess(e:MouseEvent):void {
               if (guessNumber==Number(yourGuessText.text)) {
                    outputText.text = "You are GUESS!!!";
                    points += 100;                   
                    winGame();
               } else if (guessNumber > Number(yourGuessText.text)) {
                    points -= 20;
                    pointsView.text = String(points);
                    outputText.text="need MORE, try again please";
               } else if (guessNumber < Number(yourGuessText.text)) {
                    points -= 20;
                    pointsView.text = String(points);
                    outputText.text="need LESS, try again please";
               }
          }
         
          private function winGame():void {
               pointsView.text = String(points);
               if (points >= 200)
               {
                    points = 0;
                    level += 1;
                    lastGuessNumber += 10;
               }
              
               if (points>=200 || level==4)
               {
                    trace("you are winner!!!!!!!!!!!!!!!!!");
                    youAreWinner();
               }
              
               playAgainBtn.enabled=true;
               guessBtn.enabled=false;
               playAgainBtn.addEventListener(MouseEvent.CLICK, playAgain);
          }
         
          private function playAgain(event:MouseEvent):void {
               outputText.text = "";
               startGuess();
          }
         
          private function youAreWinner():void
          {
               gotoAndStop('p2');
          }
     }

}

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Looks like the object playAgainBtn is not present in frame 2.


2 replies

Andrei1-bKoviICorrect answer
Inspiring
April 6, 2010

Looks like the object playAgainBtn is not present in frame 2.


sergey_landar
Inspiring
April 6, 2010

Yes, its help.

One question about this button.

I have a private function winGame() and there i have 'if' and in the end i have some other code like playAgainBtn.enabled=true;

So, i want go to youAreWinner() if (points>=200 || level==4) and i don't want to change all other code in the end of function..

Something like 'if (points>=200 || level==4)' then go to youAreWinner(), not more.

How i can do it? is it possible?

What about line 41?

Inspiring
April 6, 2010

I am not sure I understand the question.

What about line 41?

sergey_landar
Inspiring
April 6, 2010

after 'permit debugging' i see:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at GuessNumber/winGame()
    at GuessNumber/checkingYourGuess()