Common AS3 Errors

Despite their seemingly over-abundance while getting started with AS3,  the new error messaging is extremely helpful and specific. While error messages are tedious at first, they eventually become extremely helpful once you understand what they mean. If you make a mistake, they tell you exactly what you did wrong.

So, here’s a short list of errors that I commonly encounter due to routine code clumsiness.

A. Compiler Errors (will prevent a movie from publishing.)

1067: Implicit coercion of type [datatype] to an unrelated type [datatype].

While the wording is fancy, the nature of this error is extremely simple: you’re trying to put a square peg into a round hole; or in technical terms, your data types don’t mesh. An example scenario that would cause this error is this:

var testing:int = new Date();

Notice the problem? The “testing” variable is typed as an “int” but is getting a “Date” object assigned to it. AS3 is very strict about data types, so will force you to make sure they always line up.

1120: Access of undefined property [name].

You’re trying to reference a value that was never created. If you get this while referencing a property of another object, you’re probably trying to access a property that is not defined as a public member of that object’s class. If you get this error in response to a value that you’ve defined within the script that you’re writing, then you probably forgot to declare the value with a “var” statement before its first reference.

1151: A conflict exists with definition [name] in namespace internal.

You’re trying to declare a value that has already been created. Make sure that you haven’t declared multiple variables by the same name within the same script. A common “gotcha” that I get hit with comes from using the same iterator for multiple loops, like so:

function errorDemo():void
{
    var list:Array = new Array(0, 1, 2);

    for (var i:int = 0; i < something; i++) {
        // do something.
    }

    for (var i:int = 0; i < something; i++) {
        // do something else.
    }
}

Notice the error? I’ve declared the “i” iterator variable with a “var” statement twice. To fix that, I’d need to use a different iterator name for the second loop, or else just declare the second iterator as “i = 0”.

B. Runtime Errors (will NOT prevent movie publishing; occur at runtime.)

Error #2006: The supplied index is out of bounds.

You’re trying to reference a depth (index) within an object’s display list that does not exist. For example, you’ll encounter this error if you call “removeChild(2);” on a display list that only has one child object. The third child that you referenced there (yes, third) does not exist. Keep in mind that display lists are zero-indexed, so the lowest item in the list has an index of “0” and the highest item in the display list has an index of “numChildren-1”.

Error #2007: Parameter child must be non-null.

You’re trying to supply a child-accessor method (addChild(), removeChild(), getChildAt(), etc) with a null object reference. An example scenario that would cause this error is this:

var child;
removeChild(child);

While the above example will compile, it will fail at runtime because the child reference is not populated with a valid DisplayObject target before being used in a child-accessor method.

Error #2025: The supplied DisplayObject must be a child of the caller.

Tricky one. The foundation principal behind this error is the definition of a parent-child relationship: a parent object contains a child object within its display list RIGHT NOW. As soon a child is removed from its parent’s display list, their parent-child relationship is broken. So, this error occurs when you perform an action like using “removeChild()” on an object that is not contained within the target’s display list. In other words, an object cannot remove a child that it does not contain. There are several patterns that you can use to avoid conflicts. Among my most common is this:

var child:DisplayObject;
if (child != null && this.contains(child))
{
    removeChild(child);
}

The above pattern will avoid both a #2007 and #2025 error, since we’ve validated that the child both exists and that it is contained within our display list before trying to remove it. Just be sure to test that the object is not null FIRST, otherwise the “contains()” operation could throw a #2007 error.

Another common cause of the #2025 error is when a child-accessor method tries to work with children that are contained within another object’s display list. It doesn’t work. Child-accessor methods only work on the callee’s own children. So, when working between object scopes, I find the most reliable method to work with child displays is to call all operations relative to the child, like so:

child.parent.removeChild(child);

In the above example, we can call this operation from any scope that has a reference to the child object, and we’re sure that the operation will target the correct parent.

Advertisements

8 comments so far

  1. Brian Sexton on

    Another way to get code 1151 errors is to use the time-saving yet frequently conflict-causing “Automatically declare stage instances” feature (identically named instances in *different* classes conflict if they are within the same package).

  2. bigmac on

    Ah yes… excellent point. Good old “automatically declare instances”, causing all kinds of confusion if you don’t know what it is or what is does. Set it once when you start a new Flash project, keep it consistent between all project files, and never change it!

    I personally prefer to keep auto-declarations disabled so that I can declare all stage instances within a class. I think it’s very confusing if you have a class with a mystery property that you can’t locate within the class’ scope or any of its heritage.

  3. Sheryl on

    Thank you for taking the time to explain the error messages. Some of the errors on the list are so specific that I’ve never been able to understand what they mean, i.e., implicit coercion. And I speak excellent English. I suspect that whoever wrote those doesn’t speak English. However, you do, and what you wrote has been extremely helpful. I still get the errors and still have problems solving them, but at least I now know what the heck they mean.

    In gratitude, Sheryl

  4. bigmac on

    glad it was helpful!

  5. Chopra on

    New to AS. Loved your solution for 2025. worked like a charm. Now I need information on Error 1009. Can you please help?

    • bigmac on

      Ahhh…. error #1009. “Cannot access a property or method of a null object reference”. Yep, this is an incredibly common error, and there is unfortunately no easy avoidance to guard against it like with #2025. Ultimately, pretty much any object reference can turn up as null. It would be really helpful if the Flash runtime would report on which value within a script resolved as null, but it doesn’t give you anything but the call stack method in which the error occurred. Well, this is at least something… so, my best advice is to follow the error report to the method in which the null value was found, then trace all object references therein until you find the null reference (or you can use the Flash debugger). Also, another little tip -– generally the 1009 error is called in response to calling a method or accessing a property of a null object reference, so that’s generally a good place to start: check out all object references that are having an action performed on them.

      I also recommend being very liberal with your null tests. As you work with AS3, you generally get a feel as to where null references are introduced, how they can be avoided, or where they CANNOT be avoided; at which time you can insert manual tests to see if a value is null before doing anything with it. The basic null test is: (myVar != null) …or simply: (!!myVar). When a null test alone is not enough (like when chaining methods that all have the potential to yield a null value), then just wrap the situation in a try/catch block to handle any potential errors.

  6. David on

    Very nice! I had this error and was scratching my head trying to figure it out. Luckily, there was Google where I found this helpful article. Thanks a million!

  7. Enrra on

    This article helped me solving a problem with a #2025 error. I used your code to find out the problem. Thank you so much. :D


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: