AS3 Pitfalls – Error #1009

PROBLEM:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

SOLUTION:

i don’t know.

Error #1009 seems to be catch-all error that Flash throws whenever you reference something that doesn’t “exist”, or hasn’t been loaded into memory. A quick Google search shows that the error spans both Flash and Flex, and can crop up in a number of hair-pulling situations. Here’s the recipe i followed to produce my very own Error #1009 while building a multi-user chat application with ElectroServer 4:

1. i create a document class called Main.as and linked to it from the Flash IDE.

2. i set up my Flash swf with two frames. Frame 1 has a Login widget.

ElectroServer Simplechat Frame 1

Frame 1. Nothing fancy.

3. Frame 2 has a Chat window, a Send button, and a List component labelled “Users”. The List component’s instance name was userListClip.

ElectroServer Simplechat Frame 2

Frame 2. Note the “Users” List component on the right.

4. Once the player logs in, i tell the swf to gotoAndStop(2);

5. Now i try to populate my List component, userListClip, with the user names of the people in the chat. No good – dreaded Error #1009 rears its ugly head.

After a bit of research, i read somewhere that my code cannot manipulate objects that i’ve placed on the stage further along the timeline, because those objects have to “exist” at the moment my code is compiled. Huh. Really?

CRUMMY SOLUTION #1 – Alpha/Siberia

One solution is to have that troublesome userListClip instance exist on Frame 1, but drag it off the stage or set it to alpha 0%. This is a very clunky workaround and i don’t like it.

CRUMMY SOLUTION #2 – Clunky Listener

Someone in a Flash user group suggested i register a listener with Event.ADDED. Here’s how that goes:

1
addListnener(Event.ADDED, onAdded);
1
2
3
4
5
6
7
8
9
10
11
12
private function onAdded(e:Event):void
{
     var clipName:String = e.target.name;
     switch (clipName)
     {
           case "userListClip":
                    populateUserList();
                    break;
           default:
                    break;
     }
}//onAdded()

No WAY. That can’t possibly be a respectable solution. Let’s just say it’s “inelegant” and leave it at that. And for some strange reason, my userListClip instance doesn’t ever show up in this function. What gives? Do components not trigger Event.ADDED?

CRUMMY SOLUTION #3 – The Hands-Off Approach

Another solution, obviously, is to build the userListClip List component out of code. i’m resistant to that because i’m one of these hands-on designer types who likes to see what he’s building. i don’t like the fact that my whole design exists in this ethereal, unimaginable codespace that exists only in my darkest desires until compile time.

Not only that, but pure code solutions become a pain to troubleshoot months down the road. i’d much rather see something on the stage and click on it to see its instance name, than to pour through pages and pages of code trying to learn the programmer’s naming convention (even if the programmer is me. i have a lousy memory).

THE BEST SOLUTION

If you’ve generated Error #1009 and have lived to tell about it, please post your solution!

6 thoughts on “AS3 Pitfalls – Error #1009

  1. Marcus

    HEheehehehehe…

    I have the SAME problem too but i don´t know any ellegant metod to solve this, but i liked the funny way you told us about this fuckin shit horrorible problem =]

    Reply
  2. Spencer

    I have been having this problem for some time. My usual solution was to set the alpha to zero. But recently I have been using removeEventListener’s to some success.
    I.E.
    var score1:int = 0;
    addEventListener(Event.ENTER_FRAME,whatever); //creating a random event listener
    function whatever (e:Event):void {
    if(ball.hitTestObject(wall)==true) {
    score1++; //if hittest then score goes up by 1
    }
    if(score1==20) {
    removeEventListener(Event.ENTER_FRAME, whatever);
    gotoAndStop(2);
    //if score1 is equal to 20, then go to and play the second frame. The difference is with the remove event listener, the list clip is removed. This is only happening in the if statement, so it wont effect the movie.
    }
    }
    -spence

    Reply
  3. saurabh

    I’ve lost a sizeable patch of hair to this. And in my conquest to get on top of this one, I will admit I ended up going for the ‘CRUMMY SOLUTION #2 – Clunky Listener’ every time. And yes, that ubiquitous compiler in yr head, goes “no way!” even while you type that code out. You just know this is real real bad code! But what to do. It makes it work.

    Reply
    1. Ryan

      saurbh – i’ve had to fundamentally change the way i build things to accommodate this change. i can’t say i have it 100% figured out, because now i’m designing interfaces with code, and it takes a much longer time, becasue i can’t see the layout. Another cheap trick that i didn’t mention is instead of sending the playhead to another frame, you can have all required elements on the screen, and just give them alpha 0. Through code, you alpha 0/alpha 100 those elements. But this, too, is a big messy cheat.

      Reply
      1. player_03

        That’s actually not TOO bad. Certainly it’s not as bad as using the timeline. (It’s better to use the “visible” property, but that’s not the important thing.)

        The important thing is you want to be able to hide and show the objects as a group. If you try to hide/show the objects individually, what happens if you forget one?

        Fortunately, Flash provides a useful way of grouping display objects: put them inside other display objects! Then you can say “menuContainer.visible = false” and “chatContainer.visible = true,” which will hide everything you’ve assigned to the menu and show everything you’ve assigned to the chat interface.

        For more information, look up Adobe’s “Working with display object containers” article.

        Reply
  4. player_03

    Error #1009 occurs when a reference is null. Or as Ryan put it, when the object doesn’t exist. To solve it, follow these steps:

    1) Go to the line that’s causing the error. (The error information should tell you which line this is. If it doesn’t, go to File -> Publish Settings -> Flash and check “Permit Debugging.”)

    2) Look at the problem line. This line contains a period. If it doesn’t, check the line number again because you’re looking at the wrong line.

    3) Look at the variable name BEFORE the period. This variable is null. Don’t believe me? Add “trace(variableName);” on the line above. It will trace “null.”

    3.5) If there are multiple periods on the same line, use trace statements to determine which variable is null. Make sure to test the variables in order; otherwise your trace statement will itself fall prey to error #1009.

    4) Now that you know which variable is null, wrap the line with “if(variableName != null) { … }” This will make the code run only when the variable exists. And if the same variable gets used on the lines afterwards, move those lines into the “if” block as well.

    5) If you’re not lazy, figure out why the variable is null in the first place, and fix the source of the issue. (For example, you might be using Flash’s timeline. In that case, look up Ryan’s “Understanding Classes in AS3” series, because you don’t want to be using the timeline.)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *