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