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

**URL:** <https://forum.kirupa.com/t/cannot-access-a-property-or-method-of-a-null-object-reference/302937>\
**Category:** Uncategorized\
**Created:** [January 10, 2010, 9:39pm UTC](https://forum.kirupa.com/t/cannot-access-a-property-or-method-of-a-null-object-reference/302937 "2010-01-10T21:39:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danblukk](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@danblukk](https://forum.kirupa.com/u/danblukk)\
**Post date:** [January 10, 2010, 9:39pm UTC](https://forum.kirupa.com/t/cannot-access-a-property-or-method-of-a-null-object-reference/302937/1 "2010-01-10T21:39:32Z")

</div>

Hey  
I’m pretty new to action script. I’m trying to create a simple user name and password screen which will link to a menu and in the menu will have a choice of three games to play.

frame 1 is the logon screen. Frame 2 the menu. Frames 3,4 and 5 the games.  
I managed to create the logon screen, the menu and the first game but the error below appears when clicking on the button to go to the first game in frame 3

Cannot access a property or method of a null object reference.  
at logon\_Scene1\_fla::MainTimeline/init()  
at logon\_Scene1\_fla::MainTimeline/frame3()  
at flash.display::MovieClip/gotoAndPlay()  
at logon\_Scene1\_fla::MainTimeline/gotoFrame3()

Here is the code for the logon screen which works fine:

stop();

enterBtn.addEventListener(MouseEvent.CLICK, loginFunction);  
function loginFunction(e:MouseEvent):void{  
if(UserName.text == “dan” && Password.text == “dan”){  
gotoAndStop(2);  
}else{  
var fmt:TextFormat = new TextFormat();  
fmt.color = 0xff0000;  
Messagetxt.text = “Incorrect username or password”;  
Messagetxt.setTextFormat(fmt);  
}  
}

Code for menu screen in frame 2.

stop();

LogOutBtn.addEventListener(MouseEvent.CLICK, gotoFrame1);

function gotoFrame1(e:MouseEvent):void{  
gotoAndStop(1);  
}

Numbers\_Btn.addEventListener(MouseEvent.CLICK, gotoFrame3);  
function gotoFrame3(e:MouseEvent):void{  
gotoAndPlay(3);  
}

Spinners\_Btn.addEventListener(MouseEvent.CLICK, gotoFrame4);  
function gotoFrame4(e:MouseEvent):void{  
gotoAndStop(4);  
}

Bubbles\_Btn.addEventListener(MouseEvent.CLICK, gotoFrame5);  
function gotoFrame5(e:MouseEvent):void{  
gotoAndStop(5);  
}

and the code for the first game which is just a simple number guessing game in frame 3.

stop();

var beginMessage:String;  
var randomNumber:uint;  
var my\_guess:uint;

function init():void {  
play\_again\_btn.enabled =false;  
guess\_btn.enabled = true;

beginMessage=“Choose a number between 1 - 100.”;  
message\_txt.text=beginMessage;

message\_txt.restrict=“0-9”;

input\_txt.text="";

randomNumber = Math.floor(Math.random() \* 100 + 1);

guess\_btn.addEventListener(MouseEvent.CLICK, yourGuess);

}  
function yourGuess(event:MouseEvent):void {  
my\_guess=uint(input\_txt.text);

if (my\_guess \> randomNumber) {  
message\_txt.text = “Your guess, " + my\_guess + " is too high.”;  
}  
else if (my\_guess \< randomNumber) {  
message\_txt.text = “Your guess, " + my\_guess + " is too low.”;  
}  
else{  
message\_txt.text = "Well done, the number is " + randomNumber + “.”;  
winGame();  
}  
}

function winGame():void{  
guess\_btn.enabled = false;  
play\_again\_btn.enabled = true;  
guess\_btn.removeEventListener(MouseEvent.CLICK, yourGuess);  
play\_again\_btn.addEventListener(MouseEvent.CLICK, guessAgain);  
}

function guessAgain(event:MouseEvent):void{  
init();  
}  
init();

I know the problem exist the in init function. If I run the game in a new program, just 1 frame, it works fine, but with mutiple frames and scripts, the error. I’ve double checked all buttons and text boxes and everything is in place and named correctly.

an anybody help please :hurt:
