Error 1009 in AS3 - actionscript-3

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";

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);

AS3: Access button from class

So im quite new to AS3 but have worked with AS2 a lot before.
I have created a button and placed it on my stage then inside my class i have added this:
test.addEventListener(MouseEvent.CLICK, buttonClicked);
function buttonClicked(ev:MouseEvent):void
{
trace("Clicked");
}
Now this does not work as it can't find the stage, the only way i can get this to work is if i put the listener on the same frame as the button & not in the class.
But there must be away around this.
Thank you.
Eli
Update - adding Error messages
If I keep the above code all in the external class these are the errors i get.
Line 22 1120: Access of undefined property test. Line 22 1120: Access
of undefined property myButtonClick.
If you have created a document class with timeline then your "test" button must be in first frame. Because document class starts executing from first frame. You can access your button instance only when its available in stage.
Oh, I forgot to mention. You have to declare those instances as public variable in your document class.
public var test:SimpleButton;
Please go thru below and let me know which of the way you were having.
1) Are you having Document class?
There is a field Class in the Document Properties under the Publish tab of the flash IDE, if you are giving your class name in that field then it is called as Document Class
If you are having document class then you can create listener to your button even in the constructor button. Flash won't throw any errors like you got.
2) If you are instantiated your class in the first frame, it won't have the properties of stage till you add that to the stage using addChild. Also it won't have access to your button. And so it will throw the error, The access of undefined property.
Have you assigned the instance of the button on the stage the name "test"? The error message you posted seems to say there is nothing with the name "test" to assign the event listener to.
To check, click on the button the stage and look at the 'Properties' tab: will show in a text box near the top if it needs assigning.
Now the second error you posted means you're referring to something called "myButtonClick" without first declaring/initialising a variable/function with that name. You will either need to declare it or correct it if you meant to refer to something else.
Fixed.
I was being rava stupid as normal, forgot to put them inside the init :|
For the people who might come across this problem.
Working Code:
public function Main()
{
// constructor code
test.addEventListener(MouseEvent.CLICK, myButtonClick);
}
function myButtonClick(ev:MouseEvent):void
{
trace("button Clicked);
}
Anyway thanks guys for the help sometimes its just the simplest answers are the correct ones.
Eli

MovieClip on Flash stage does not re-instantiate when leaving keyframe and returning

I've been debugging the following issue for quite awhile now and have hit a wall.
I've set up a project in Flash (CS4, btw) that has a set of keyframes that I move between to represent the various screens of a game. One of them has a MovieClip defined (with children inside it) representing an option menu, that appears on a couple of different keyframes.
The problem I'm having is that this MovieClip reference seems to be accessible when I first enter the keyframe (using "gotoAndStop"), and occassionally when I move to other frames and back. But in at least one case, when I exit the frame and come back, I get a null reference error (TypeError: Error #1009: Cannot access a property or method of a null object reference). when I try and access it (getChildByName("optionMenuTitle")). I've even tried having the system iterate from 0 to numChildren and print out the name of each object, but it returns NULL at position 7 despite returning numChildren as 9. Does anyone have any idea why this particular MovieClip reference is NULL only in this case??
Here is a basic (abbreviated) rundown of the process occurring:
//set up function to be fired on frame construction
addEventListener(Event.FRAME_CONSTRUCTED, fadeIn, false, 0, true);
public function fadeIn(event:Event):void {
_handler.handle(); //this function is called which runs the debug statement below
trace (mainDoc.numChildren); //displays 9
for (var i = 0; i < mainDoc.numChildren; i++) { trace(mainDoc.getChildAt(i).name); } //throws null when it gets to 7
optionMenuTitle = OptionMenu(mainDoc.getChildByName("optionMenuTitle")); //the original failed call that caused me to debug
}
edit: One other potentially useful bit of information. If I comment out the getChild commands above that error, the frame loads and I can see the MovieClip visually displayed on the stage (although it's not interactive and is constantly cycling through the frames of its child objects). Still can't access it programatically though.
another edit: The object in question is a subclass of MovieClip that I named "OptionMenu". I put a breakpoint in the OptionMenu constructor, and when the frame loads correctly, that breakpoint is hit. When I get the error above, the breakpoint in the constructor is never hit. The debugger doesn't seem to give me access to see what's going on inside Flash's mind when it's instantiating the frame, however, so I can't see the logic as to why the constructor is never called.
Well this one has been driving me crazy. I could not workout why it does not reference your optionMenuTitle when you go back to the frame called title a second time.
The only way I could work around it was to take the 3 buttons out of the OptionMenu MovieClip and put them on the stage with the grey background underneath, essentially doing away with OptionsMenu.
So I moved all initialization code from OptionMenu to your TitleHanlder and also added the destroy calls to your destroy method in TitleHandler for each of the 3 buttons.
I also changed the refs from root to mainDoc:
sound.initialize(LogicGameMain(mainDoc).soundOn);
music.initialize(LogicGameMain(mainDoc).musicOn);
This worked for me as you can still interact with the buttons the second time around. It definately seems like there is some bug with these buttons being nested.
I hope this is useful for you.

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.