This isn’t right… MovieClip nested inside Button throwing null object reference error in Flash CS4
A great thing about teaching is that your students approach problems in ways you haven’t done before and run into problems that you never knew existed. This is one such problem in Flash CS4 and a curious one at that.
THE PROBLEM: A Button symbol is placed on the Stage on a frame other than the first frame and given an instance name in the Properties panel. In the Actions panel, ActionScript is added that references the instance name of the button on the same frame that the button instance first appears on. The movie is tested and the following error occurs:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MovieClipInsideButton_fla::MainTimeline/frame2()
Normally this error means that a particular instance has not been given an instance name in the Properties panel on the frame where the ActionScript appears. However, nothing looks amiss in the ActionScript or on the Stage. Adding a trace statement, such as trace(myButton); reveals null in the output window, meaning the trace statement has run before the instance name has been set to the button instance on the Stage. Hmm… very perplexing, since you may have other buttons and movieclips that work fine when referenced from ActionScript.
THE SOLUTION: From what I found, one scenario will produce this error. If a MovieClip symbol (instance) appears inside a Button symbol and the button appears on any frame other than the first frame, and the frame where the ActionScript references it is the same frame it first appears on—then this behavior will result.

There are a few workarounds:
- Retrieve the instance from the display list of its parent. Instead of referencing the Button instance on the stage directly, use
getChildByName(.)to retrieve a reference to the instance, this can even be set to instance name at the beginning of your timeline code, like so:// myButton is the name of the instance as set in the Properties panel. // Retrieved child is cast as a SimpleButton, since this is the // class of the Button symbol myButton = SimpleButton(getChildByName("myButton"));
- Do not nest a MovieClip inside a Button symbol. A Button nested inside a Button, a MovieClip nested inside a MovieClip, and a Button nested inside a MovieClip work fine.
- Place the first occurance of the Button instance on any frame before the frame where ActionScript first references it. This behavior appears to indicate Flash is taking too long to parse over the MovieClip inside a Button and runs the ActionScript on the timeline prematurely. Having the instance appear on a frame prior to the frame the ActionScript appears on ensures the instance is completely present in memory before it is referenced.
- Place the instance referencing code inside an enter frame event handler function. To ensure the entire frame has been parsed before any ActionScript code is run, enclose the offending code inside the event handler function of the enter frame event, and only make it run once, like so:
addEventListener( Event.ENTER_FRAME , ensureRendered ); function ensureRendered( evt:Event ) : void { removeEventListener( Event.ENTER_FRAME , ensureRendered ); // All instance referencing code appears below this point // In this case, instance is named "myButton" on the Stage myButton.width = 200; }
This behavior doesn’t seem right to me and even seems it could be a bug. If anyone has an explanation for this behavior, I would love to hear it. You may download an example FLA here: Download Source
UPDATE September 20, 2009: I added a new workaround, which is probably the most succinct and reliable one yet to use. See Retrieve the instance from the display list of its parent in the list of workarounds.

September 17th, 2009 at 6:57 pm
Dude, firstly “code on the timeline” just sounds all bad. Secondly, have a look at Event.ADDED_TO_STAGE.
September 17th, 2009 at 7:18 pm
Hi Barry,
Code on the timeline does give me the shivers too, but it is a good way for those that have never programmed before to have a first taste.
Event.ADDED_TO_STAGE is a good approach I think, but from my experiments it doesn’t pick this up either. There is a possibly related problem of losing references to instances on the stage from external AS3 classes that use gotoAndStop(.): http://richardleggett.co.uk/blog/index.php/2008/02/18/enabling_access_to_timeline_items_in_as3. In this case Event.RENDER is used as a workaround. I just wonder if there’s an explanation for this behavior or if it is truly just a bug.
September 17th, 2009 at 7:26 pm
Oh, I should just note the reason I haven’t been able to get Event.ADDED_TO_STAGE to work is that since the reference to the instance is lost, I can’t add an event listener to the button itself. The Event.ENTER_FRAME works because it is not attached to the missing instance, however this too fails if there is not a stop(); on the frame.
November 2nd, 2009 at 7:17 pm
Thanks so much! Been searching for a solution for hours today — it was as simple as copying the button frame (frame 75), pasting it in the following frame (frame 76), and moving the action script to the latter frame (frame 76) (workaround 3). You’re a genius — thanks!
November 25th, 2009 at 10:03 pm
omg i love you so much. been annoyed with this all night. the 3rd workaround worked best for me.
cheers
December 14th, 2009 at 2:20 pm
Thanks a bunch for this. This one little error has been holding me up for a while now.
January 14th, 2010 at 4:06 am
Hi i also just tried your third workaround and it did the trick.
I just don’t know how to thank you enough for this tip, as this has puzzled me and kept me awake all night many times. This is such a relief to learn it wasn’t all me!!! I knew i wasn’t referencing something that wasn’t there! Thank You thank you thank you!! Now i can go back to using mc’s in my buttons without fear!
January 15th, 2010 at 12:00 am
Cheers Steve, glad it helped you out!
January 15th, 2010 at 6:20 pm
Thank you!!! I looked for this problem 2 houres and that’s the solution!!!! Thanks a lot!!!
January 22nd, 2010 at 3:21 pm
The problem might be this:
mouseChildren = false;
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/DisplayObjectContainer.html#mouseChildren
“This property is useful when you create a button with an instance of the Sprite class (instead of using the SimpleButton class). When you use a Sprite instance to create a button, you can choose to decorate the button by using the addChild() method to add additional Sprite instances. This process can cause unexpected behavior with mouse events because the Sprite instances you add as children can become the target object of a mouse event when you expect the parent instance to be the target object. To ensure that the parent instance serves as the target objects for mouse events, you can set the mouseChildren property of the parent instance to false.”
Also noted here:
http://numiko.com/labs/?p=223
February 9th, 2010 at 6:03 pm
Thank…you…so…much
I’ve been pulling my hair out over this issue for hours on end. Tried the third solution, worked for me.
February 9th, 2010 at 7:10 pm
[...] http://blog.anselmbradford.com/2009/09/10/this-isnt-right-movieclip-nested-inside-button-throwing-nu... [...]
February 19th, 2010 at 2:33 am
how can work btn whthin movieclip ,can send me a fla file
March 9th, 2010 at 10:20 pm
This also happens in CS3. That’s an AS3 bug perhaps within the Timeline Object.
March 16th, 2010 at 4:54 am
Thanks a heap for posting! You saved me from insanity… I had a bunch of buttons on a Frame and I was randomly getting errors when adding event listeners because of nulls, but almost always on a different button! I’ve put the initialisers in an enterFrame listener and it works great
April 10th, 2010 at 11:53 am
Thanks been struggling with this error for a good 24 hours now, Didn’t know you can’t mix a movie clip into a button, I got a question too???? is there a way you can make the button animation if you can’t put a movie clip in it???? because I replace the animation movie clip with a shape object but I want it to animate, sorry i am a beginner and havn’t learning script so just using object and simple techniques that works thnx if you can help
April 10th, 2010 at 3:41 pm
Hi Nunu,
Try following the workarounds above while having an animated movieclip inside the button.
April 16th, 2010 at 8:28 am
Pls help me out with this…
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at dbarkadz_fla::MainTimeline/frame1()
The code is:
function buttonb_clicked(event:MouseEvent):void{
gotoAndStop(“index”);
Event.ENTER_FRAME
}
buttonb.addEventListener(MouseEvent.CLICK, buttonb_clicked);
April 16th, 2010 at 9:36 am
Click on your button symbol on the stage and check the Properties panel. Is it named “buttonb” in the “Instance name” field? Also, “Event.ENTER_FRAME” shouldn’t be in the “buttonb_clicked” function.
May 5th, 2010 at 10:08 pm
thank you so much!!!! I am in a Flash class at school and have had the most difficult time getting my button to work. I was told to put my actionscript in the same frame as my button appears but would always get an error message. After reading your page I moved my actionscript one frame later in the timeline after my button appears and it works great!
May 6th, 2010 at 6:25 pm
I too have spent the better part of the day trying to solve this problem and have seen more increasingly complicated ways to deal with it until this. Just move the start frame of the code! Brilliant! Can’t believe this is the only reference to these issues. THANK YOU, SIR!
July 29th, 2010 at 12:07 pm
Holy crap. This is a bug in Flash. You should be able to put a movieclip instance inside a button … with no such problems. Thanks for your post … inform Adobe. These workarounds should not be required, and neither should using ADDED_TO_STAGE. Thanks again.
January 19th, 2011 at 3:25 pm
Thanks so much for your workarounds. I too teach simple Flash Actionscript coding (on the timeline) to GCSE students and this error was driving me mad. I found that moving the actionscript one frame to the right (your 3rd workaround) is the simplest for the students to grasp. Thanks again for your superbly concise explanation of the problem – I’ve trawled dozens of other sites and your explanation of the problem + solution is superb.
January 19th, 2011 at 3:42 pm
Cheers David,
Glad it worked for you, it’s unfortunate to have to give students a workaround for a software issue. Look me up on Linkedin, I always like to connect with others teaching ActionScripting.
March 29th, 2011 at 1:42 pm
Thanks a lot!!!!
I’ve been having this problem in a project for a while.
I used your suggestion myButton = SimpleButton(getChildByName(myButton)); and it’s ok now!
Thank god!
I lost hours!
April 2nd, 2011 at 10:26 am
Thank you for your help! I’ve encountered this problem twice and found that only your site offered the help I needed!!
You saved me tons of time!
April 12th, 2011 at 11:58 am
WOW! Thank you so much for this!! I was starting to start pulling my hair trying to figure this one out. It appears Flash does not render the instance before the instance is referenced by actionscript and that’s where all problems arise form. ENTER_FRAME event listener did it for me.
April 30th, 2011 at 4:56 am
It think my self a genius to solve every error, But after reading this I just think that i am nothing. Thank you for solving such a complex problem. Complex because i already spend 32 hour or more in finding and solving this problem.
Once again Thanks a lot
June 10th, 2011 at 4:11 am
Dude,
thnx found the freaking problem..
Bless
July 16th, 2011 at 3:47 pm
Hey!
I have been developing flash games for a few years and I have run into this error a number of times. In fact, this error CAN HAPPEN TO MOVIECLIPS INSIDE MOVIECLIPS! While usually this is not the case, nesting movie clips is always a risk, especially if you are nesting more than one level, as in you have a parent movieclip with a child that also has a child and you are moving around the top parent’s frames. The problem is that the code tries to reference an object that has not loaded, so the quick and easy solution is to export your movieclips by making them AS classes, even if you don’t have an actionscript class written for them. I do this for all my large-size movieclips and it solves the problem. For example, when I make a level movieclip with different frames for each stage of my game, and and inside each stage is a number of objects that have children themselves, I always export the parent level MC and it fixes the issue every time!
Hope that helps,
Jon
July 16th, 2011 at 4:09 pm
Hi Jon, thanks for the tip!
October 17th, 2011 at 2:17 pm
THANK YOU SO MUCH. I’ve been battling this for days- I wanted different movie clips to play on up, down, and over… but I kept getting that stupid error. I ended up putting the action script on the next frame over, and it worked like a charm. You are my HERO!
October 25th, 2011 at 6:04 am
I JUST LOVE YOU!!! you saved me from suicide!!!
October 25th, 2011 at 6:21 am
Donutman, heh, glad I could help