TypeError: Error #1009: Cannot access a property or method of a null object reference. When trying to create a button. - actionscript-3

Heyy, Im having some problems with my buttons. What I have is 4 buttons on the page. They will run an animation while not being pressed, and that animation will change when it is being pressed.
I keep getting the error TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MultimediaAssignment_fla::MainTimeline/frame51()[MultimediaAssignment_fla.MainTimeline::frame51:3]
And I have searched high and low but I cant seem to find anyone who is having the same problem as me.
stop();
btn_volcano.addEventListener(MouseEvent.CLICK, volcano);
btn_Storm.addEventListener(MouseEvent.CLICK, stormbtn);
btn_Tsunami.addEventListener(MouseEvent.CLICK, tsunamibtn);
btn_Earthquake.addEventListener(MouseEvent.CLICK, earthquakebtn);
function volcano(e:MouseEvent):void {
gotoAndPlay(52);
}
function tsunamibtn(e:MouseEvent):void {
gotoAndPlay(54);
}
function stormbtn(e:MouseEvent):void {
gotoAndPlay(53);
}
function earthquakebtn(e:MouseEvent):void {
gotoAndPlay(55);
}
My buttons do have correct instance names and for some reason it worked when I turned the "up" movie clip into a button and called it e.g. btn_volcano. Except if I do that the animation doesn't run anymore.
If I remove the instance name from in front of the event listener I don't get the error but it will only ever run the last code.
Please help!

error TypeError: Error #1009: Cannot access a property or method of a null object reference. at MultimediaAssignment_fla::MainTimeline/frame51()
This line suggests you have some code in frame 51. I presume in Flash IDE in main timeline.
Click on this frame on timeline and open actions window. Problem may be there.

Related

adobe flash show/hide different movieclips in different frames with one button Error #1009

I created a button to a layer and I am trying to show movieclip com7 in frame 1 when I click the button named quest. Then, I would like to show a different movieclip com9 in frame 2. I put the movieclips in another layer each one in frames1 and 2.
In frame1 the code is:
quest.visible=true;
com7.visible=false;
quest.addEventListener(MouseEvent.CLICK, q7_clicked);
function q7_clicked(event:MouseEvent):void
{
if (com7.visible==false)
{com7.visible=true
}
else
{
com7.visible=false;
}
}
in frame 2:
quest.visible=true;
com9.visible=false;
quest.addEventListener(MouseEvent.CLICK, q9_clicked);
function q9_clicked(event:MouseEvent):void
{
if (com9.visible==false)
{com9.visible=true
}
else
{
com9.visible=false;
}
}
Flash creates the swf without errors but when I click the button in frame2 there is a TypeError:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q7_clicked()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at meli_fla::MainTimeline/q9_clicked()
The issue is that when you add event listeners on the timeline, those listeners do not automatically go away when you change frames (no code does).
So on frame 1, you just have the one listener and it probably works fine.
On frame 2, you create a new listener, but the one from before on frame 1 is also still hanging around, so when you click the quest button, it actually calls q7_clicked and q9_clicked. No matter what frame you are one, at this point clicking the button you added the listeners to will always call both functions.
Your error, is because the objects you're referencing (com9, com7) are likely not around on both frames you are visiting (confirmed from you comments on the question).
To remedy the problem, you need to remove the appropriate event listener when you move to a new frame.
So, wherever in your code you do nextFrame(); or gotoAndStop(2); or however you move the user to another frame, at that time remove the listener on the button:
quest.removeEventListener(MouseEvent.CLICK, q7_clicked);
gotoAndStop(2);
Or, if returning to frame 1:
quest.removeEventListener(MouseEvent.CLICK, q9_clicked);
gotoAndStop(1);

How to fix an error #1009 in a class added to Main?

I have a class called ChestScene that represents a scene/MovieClip in a .fla file I'm working on. I've had tons of issues that seem to be rooted in a fundamental misunderstanding of how to properly use objects that have code attached to them. It is my understanding that adding the object to the stage automatically instantiates it, so right now all I'm trying to do is instantiate and add to stage ChestScene() in the constructor of my Main method. I thought it would be as simple as this:
public class Main extends MovieClip {
var chest:Chest = new Chest();
public function Main() {
stage.addChild(chest);
}
But I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Chest()/startScene()
at Chest()
at Main()
So my first question is why is Chest null? The object exists in my fla. If I add it to the stage by dragging from the library, the class works as intended. addChild seems to work on other objects that I am using the same way so I don't get why I can't use it on this object.
How to use the Main method to instantiate an object and access/change properties of said object relative to the stage? And how can I do the same thing for the objects nested inside of my initial object?
***** Edit after answer
Thanks for your reply and for teaching me how to read the debug message. startScreen() was indeed the culprit, and the issue there was this:
stage.addEventListener(Event.ENTER_FRAME, gameLoop)
Removing stage and adding the listener to the object fixed that error, so kudos! But I'm still confused; why does trying to add the listener to the stage cause a null error? I don't understand why the stage would be null at this point. Also, removing "stag." from the addEventListener caused the size of the frame that holds the scene to be way smaller, which cropped a lot of my image. Why is an event listener affecting my stageWidth and height? This is why I also asked questions about how to correctly position things relative to the stage width and height, because I know it has something to do with the errors.
why is Chest null?
Chest is not null. It's the constructor of the class and cannot be null.
You should pay attention to the Stack trace attached to the error (the lines with "at …" below the message).
It tells you where the error occurred. Reading it from the bottom up gives you the order of method calls performed.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Chest()/startScene()
at Chest()
at Main()
Main() called Chest(), Chest() called startScene() and BOOM! this is where the error occurred.
To debug this error look at startScene() . Something in there is null.
Looking at your current code:
stage.addChild(chest);
I guess that you also try to add something to stage in Chest(). But stage is not available ( null) in Chest().
In general, it's bad practice to add things to stage as can be read in the documentation of addChild()
Simply add to the object and not stage :
addChild(chest);

Actionscript 3: gotoAndStop

I have a project in Flash CS3 that's giving me a bit of trouble. I have a movieclip, and in that movieclip, I have a button. The movieclip is named bg and the button tohallway_btn. My coding is on the stage on a layer, not on classes or in a package, or anything of that sort. This is my coding:
bg.tohallway_btn.addEventListener(MouseEvent.CLICK, tohallwayClick);
function tohallwayClick(event:MouseEvent):void{
gotoAndStop (141);
}
It seems simple enough, yet when I debug and the button is clicked, the flash player freezes over. I have absolutely no idea what's causing it to do this.
I get a type error in output as well:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Camille_fla::MainTimeline/enterF()[Camille_fla.MainTimeline::frame140:130]
Any help is appreciated.
An onEnterFrame listener was called and not removed that was referencing an object (bg) that was not on the stage after the goto call.
function tohallwayClick(event:MouseEvent):void {
**removeEventListener(Event.ENTER_FRAME, enterF);**
gotoAndStop(141);
}
First make sure your button and your code are on the same frame, they can be on different layers, but make sure they are lined up.
If you want it to go to the frame on your main timeline, or stage, instead of writing:
gotoAndStop (141)
try:
stage.gotoAndStop(141);

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

This is quite frustrating. I am simply trying to create a dynamic text and put some text into it on runtime.
I get this error though
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MethodInfo-1()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
I have a text object named textLabel, which is inside a movieclip named MC_state.
I get it because I use:
MC_state.textLabel.text = "asdasd";
And I wish I knew what the problem was. I have other objects set up quite the same way which don't give me that problem. I just don't know how debug that.
Thanks!
The error is telling you that there is no object somewhere along MC_state.textLabel.text, so either flash cannot find MC_state, or textLabelinside MC_state or (unlikely) text inside MC_state.textLabel.
If I may venture a guess though, I think you're seeing this because this happened:
you have somewhere a movieclip called MC_state that has more than one frames. You tied to do gotoAndStop or gotoAndPlay to a frame that has the textfield called textLabel and that's the text you're trying to change.
The problem, and it comes up often for people transitioning from AS2, is that when you execute the gotoAndPlay/gotoAndStop function, the movieClip doesn't update right away, this happens iin the render phase. The code after that function however executes right away, so the movieclip is still at the old frame.
there are two ways you can handle it
set up a event handler that updates the render event, and change the text then. You can hurry the stage rendering by running stage.invalidate example
the other (better) option is to have the text in all the frames, and have it hidden or invisible, that way you can access it at any time.

Error 1009 in AS3

I have TextField instance called inputWord which contains no text on the first frame. On the same frame, on the actions layer, any time when I refer to inputWord in any way, there is an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DC/frame1()[DC::frame1:19] //DC is the name of document class that I created.
at flash.display::MovieClip/gotoAndStop()
at DC()[C:\Users\nikkka\Desktop\flash\DC.as:25]
19 is the number of line where my code that involves inputWord is located. It works, I mean I write
inputWord.text = "smth"
it text becomes "smth" but there is the same error. Why?
The problem is with gotoAndStop()
in as2, when you do a gotoAndStop you can access the resources in the frame right away, as Kevin pointed out, the frame has to be rendered first
to do this, you need to use an onrender listener to fire when you rendered the frame to deal with the frame related logic. Then you need to invalidate the stage, to force the rendering to fire.
like so:
stage.addEventListener(Event.RENDER, onRenderStage);
protected function onRenderStage(ev:Event):void {
inputWord.text = "smth"
trace(inputWord.text);
}
gotoAndStop(5);
stage.invalidate();
Probably on the first frame, inputWord is not loaded yet so you get an error. On the next frames, it is loaded so the text is being set successfully. The solution is test for the existence of the text field before setting it:
if (this.inputWord) this.inputWord.text = "smth";