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.startDrag(). Place a trace statement above this variable to see if its value is
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;
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
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