Skip to main content
Participating Frequently
Jan 25, 2025 6:57 PM UTC
Answered

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

I'm trying to create a simple quiz, and it kind of works, but it pops up that error seemingly at random and I can't wrap my head around it.

 

The quiz works by having a pool of 10 questions, each on a different frame. It starts by clicking a button, which uses this code:

 

StartQuiz.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

//A che domanda siamo nel quiz
var QNumber:Number = 0;

//frame delle domande
var Qarray:Array = [2,3,4,5,6,7,8,9,10,11];

function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
	this.PName = PlayerName.text;
	//Prendo un numero da 0 alla lunghezza dell'array
	var i:Number = (Math.floor(Math.random() * Qarray.length));
	//Quel numero è la posizione nell'array della prossima domanda
	var ProxFrame:Number = Qarray[i];
	//Che tolgo dall'array per evitare si ripeta
	Qarray.splice(i,1);
	//E mi segno a che domanda siamo per scriverla in alto e per fermarmi alla quinta
	QNumber++;
	trace("i=" + i);
	trace("QNumber =" + QNumber);
	trace(Qarray);
	MovieClip(this.root).gotoAndStop( ProxFrame, "Quiz");
}

to select from the array (which contains the numbers of the frames, so from frame 2 that has the first question to frame 11 which has the last one) one question and removing it so it doesn't come up again. Every frame has its own "proceed" button, which is hidden with "Proceed7.visible = false;" at the beginning so that the player can click on an answer and only then be allowed to proceed to the next question. Every Proceed button has a code similar to the one above:

 

Proceed5.addEventListener(MouseEvent.CLICK, NextQ5);

function NextQ5(event:MouseEvent):void
{
	var i:Number = (Math.floor(Math.random() * Qarray.length));
	var ProxFrame:Number = Qarray[i];
	Qarray.splice(i,1);
	QNumber++;
	trace("i=" + i);
	trace("QNumber =" + QNumber);
	trace(Qarray);
	if (QNumber > 5) 
	{
		MovieClip(this.root).gotoAndStop(12, "Quiz");
	}
	else
	{
		MovieClip(this.root).gotoAndStop(ProxFrame, "Quiz");
	}
}

And the thing that's making me go insane is that sometimes it works just fine, but some others the error #10009 comes up, sometimes referencing the first button, sometimes one of the proceed (not always the same, and not always on the same array output, so it's not something specific I think)

 

This is the code that hides/reveals the proceed button, if it helps:

 

this.QNum.text = "" + QNumber;
Proceed6.visible = false;
Answ6.visible = false;

//Funzioni per le risposte
RispostaB6.addEventListener(MouseEvent.CLICK, RispostaGiusta6);

function RispostaGiusta6(evt:MouseEvent):void
{
	SfondoRisposta6B.transform.colorTransform = new ColorTransform(0,0,0,1,0,255,0,0);
	Proceed6.visible = true;
	RispostaA6.visible = false;
	RispostaC6.visible = false;
	RispostaD6.visible = false;
}

RispostaA6.addEventListener(MouseEvent.CLICK, RispostaSbagliata6a);

function RispostaSbagliata6a(evt:MouseEvent):void
{
	SfondoRisposta6A.transform.colorTransform = new ColorTransform(0,0,0,1,255,0,0,0);
	Proceed6.visible = true;
	Answ6.visible = true;
	RispostaB6.visible = false;
	RispostaC6.visible = false;
	RispostaD6.visible = false;
}

RispostaC6.addEventListener(MouseEvent.CLICK, RispostaSbagliata6b);

function RispostaSbagliata6b(evt:MouseEvent):void
{
	SfondoRisposta6C.transform.colorTransform = new ColorTransform(0,0,0,1,255,0,0,0);
	Proceed6.visible = true;
	Answ6.visible = true;
	RispostaB6.visible = false;
	RispostaA6.visible = false;
	RispostaD6.visible = false;
}

RispostaD6.addEventListener(MouseEvent.CLICK, RispostaSbagliata6c);

function RispostaSbagliata6c(evt:MouseEvent):void
{
	SfondoRisposta6D.transform.colorTransform = new ColorTransform(0,0,0,1,255,0,0,0);
	Proceed6.visible = true;
	Answ6.visible = true;
	RispostaB6.visible = false;
	RispostaC6.visible = false;
	RispostaA6.visible = false;
}

 If anyone could help me understand I would be infinitely grateful!

JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Jan 25, 2025 10:10 PM UTC

Thanks for the file.

 

You have a couple of mismatches when referencing buttons in your code.

For example, in frame 5 of the Quiz scene, you are trying to add an event listener to RispostaD1, but this button doesn't exist in the same frame.

Something similar happens to frame 3.

So I suggest you to double-check the references in code and the instances on stage.

14 replies

JoãoCésar17023019
Community Expert
Community Expert
Jan 25, 2025 9:18 PM UTC

Hi.

 

Is the Quiz scene the only one you have in your FLA? What does the Scene panel show?

 

Regards,

JC

Translate
Participating Frequently
Jan 25, 2025 9:27 PM UTC

I have two other scenes (don't mind the names, I'm italian):

 

 

Risorse (Resources) can be accessed ony through the Main Menu (Menu principale), it's basically just a frame with some text. The Main Menu has a button that brings to the first frame of the Quiz scene, where the player can write their name in an input text and click on a button (the one with the startquiz functin at the beginning of my question) to start the quiz. In that first frame there's also another button to go back to the main menu, but it has never caused me any issues.

Translate
JoãoCésar17023019
Community Expert
Community Expert
Jan 25, 2025 9:34 PM UTC

Alright. Thanks for the info.

 

Do you have a FLA that we can take a look? You can use placeholder assets if needed.

Translate
kglad
Community Expert
Community Expert
Jan 25, 2025 7:04 PM UTC

enable debugging and find the line with the error.

Translate
Participating Frequently
Jan 25, 2025 7:37 PM UTC

I tried, and got

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at QuizBLS_fla::MainTimeline/frame20()[QuizBLS_fla.MainTimeline::frame20:41]
at flash.display::MovieClip/gotoAndStop()
at QuizBLS_fla::MainTimeline/NextQ6()[QuizBLS_fla.MainTimeline::frame22:73]

 

If with frame it refers to the literal adobe animate frame there's something wrong, since there is no frame 20 at all, and if it refers to the 20th line of code it refers to "MovieClip(this.root).gotoAndStop(ProxFrame, "Quiz");" which is random, since sometimes it works, sometimes it doesn't. My guess would be that it's related to the array, since it's the only random thing, but I can't understand how it's a problem and why it's a problem only sometimes

Translate
kglad
Community Expert
Community Expert
Jan 25, 2025 7:51 PM UTC

what are the few lines surrounding the problematic frame 20, line 41 (and indicate which is 41)

Translate