Common Flash Compiler Errors: #1009
My students often run into the same kinds of errors, so I thought I would post some info on the more common ones. One very common error to receive in Flash is this one:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Quick Answer and Solution
Somewhere in your code there is a variable that you are using to refer to a property or method of an object (such as a movie clip), however, it does not actually contain an object so it throws an error. Look for the parts of your code where you have a dot following a variable name and some property or method, such as myMC.width or myMC.startDrag(). Place a trace statement above this variable to see if its value is null.
trace( myMC ); // if this lists "null" in the output window the next line of code will throw an error // look for the first occurrence of myMC (where it was created) to see why it does not // contain a reference to something other than null myMC.width = 200;
Technical Overview
Objects are collections of variables and functions that are defined in a class file. In the context of Object-Oriented Programming, variables are referred to as properties, and functions are referred to as methods. A movie clip object (which is an instance of the MovieClip class) contains properties and methods for dealing with the appearance and interactivity of movie clips, such as x, y, width, height, startDrag(), addEventListener(.), etc. When a new object is created it is typically stored in a variable—technically meaning a reference to the object is stored in the variable, not the actual object—the variable acts like a unique identifier to lookup the object within a particular section of code (called the scope of the variable).
For example, a new movie clip object can be created with new MovieClip(), this would then be stored in a variable (named myMC in this example) with the code var myMC:MovieClip = new MovieClip();. Properties and methods of this movie clip object can be accessed using dot notation. For example, to return the width in pixels of the movie clip, the following may be typed:
var myMC:MovieClip = new MovieClip(); trace( myMC.width ); // outputs 0 because the movie clip doesn't have any graphics, // and is therefore 0 pixels wide
If the variable was created, but not set to reference a new movie clip object instance, it will be empty and contain the special value of null, denoting the absence of a reference to an object. Trying to access a property or method via the variable would then mean the computer would attempt to find the property or method of a null value, which being a placeholder for “no value,” throws an error.
var myMC:MovieClip; trace( myMC.width ); // throws error #1009 because the variable myMC does not contain a reference to an object

June 6th, 2009 at 5:08 am
Anselm,
Thanks for this v.clear explanation. Wondered if you’d come across anything like the following ‘null’ problem:
I have a a MovieClip that flip flops between being a MovieClip and a null object. The following code is on Frame 1 of a .fla with the bar_mc movieclip pre-placed on the stage and a large bitmap on Frame 2 to provide something to preload. Running the swf via ‘simulate download’ , the trace(bar_mc) function outputs MovieClip right up until the moment bytesloaded=bytestotal. Then it traces null. Then reverts to MovieClip. It works but I’m intrigued to understand what’s going on.
stop();
addEventListener(Event.ENTER_FRAME, myprogress);
function myprogress(e:Event)
{
var loaded = stage.loaderInfo.bytesLoaded;
var totalLoad = stage.loaderInfo.bytesTotal;
var loadedPercent = Math.floor((loaded/totalLoad) * 100);
trace (bar_mc);
if (loadedPercent == 100)
{
removeEventListener(Event.ENTER_FRAME, myprogress);
trace (loaded,totalLoad,bar_mc,root,loadedPercent);
}
if(bar_mc is MovieClip)
{
bar_mc.scaleX = loadedPercent/100;
}
}
June 7th, 2009 at 5:59 am
Interesting find. This seems like a bug in “simulate download,” since it doesn’t show this behavior when tested in real-time. The reason bar_mc is null is that on the enter frame event where bytesloaded equals bytestotal the playhead has begun moving again, so it moves to frame 2, where bar_mc doesn’t appear on the stage. It then loops back to frame 1, where bar_mc exists again. trace( currentFrame ); in your myprogress(.) function to see this, or place a stop(); on frame 2.
June 8th, 2009 at 6:23 am
Ans,
Many thanks for the reply. You’re right – I think it’s the way that ‘simulate download’ is working here that confused me…I couldn’t understand why the image was displaying as I had a stop(); on Frame 1…and had assumed it as something to do with the preloader code over-riding it some way. Now I can see that it is infact looping (ignoring the stop). Tthe trace(currentFrame) trick is very useful – will definitely add that to the armoury! Again, your reply is much appreciated. Can be v.frustrating/time consuming trying to sort these things when your learning/working solo.
Kind regards
Mike
P.S The soulofathens site is fabulous, both the way it looks and the content. Very inspiring in all sorts of ways. Haven’t looked at everything yet but ‘lost in plain sight’ is v.moving.
November 4th, 2009 at 9:58 am
Hi,
I’m working with a movie clip that is on the main timeline. It is an intro animation, located on Frame 2 of a 3-Frame Timeline. Frame 1 being a preloader, and Frame 3 being the content of a website. Here is my workflow:
Frame 1 – Preloader loads content –> Frame 2 – Intro Animation movieclip plays. When finished on the final frame, a gotoAndPlay code runs, telling it to go to –> Frame 3 – Content.
The AS3 code, located within the Intro movie clip is simply:
MovieClip(parent).gotoAndPlay(3);
And I get the type error. I have also tried running MovieClip(root) to no avail. Any ideas on where my problem might lie?
November 4th, 2009 at 10:18 am
Hi Adam,
What does the full error look like? Is it being thrown by the Intro Animation or the main timeline?
November 4th, 2009 at 10:22 am
Hey,
Well, it’s actually coming from two different locations, it would seem:
at flash.display::MovieClip/gotoAndPlay()
at index.fla::Intro_5/frame20()
Frame 20 is where the action is located. I have also tried to import flash.display.*; and import flash.events.*;
as I had seen that might be necessary on some other forums.
November 4th, 2009 at 10:43 am
Hi Adam,
This is actually the path it took to get to your error, so the error was triggered after the gotoAndPlay() method on frame20 of the intro animation. This is the only code in the intro animation? What code is on frame 3 of the main timeline?
November 4th, 2009 at 10:57 am
Ah, nevermind. I didn’t realize that it was a path. I assumed that each line was an individual type error. There’s actually an additional line on top of that MovieClip/gotoAndPlay() location. I just assumed it was linking to a separate issue, but I am in the middle of debugging that problem now as it relates to a separate issue I’m having with a scrollbar code. So, I will continue to work that out. Thanks, your help is much appreciated!