One of the more common questions I see on forums and get from colleagues is how to debug Error 1009, also known as the “Null Object Reference Error.” Or, as I call it, the “Annoying Mosquito Error From Hell.” It crops up a lot, and unfortunately the error itself does not contain much information about the source of the error. In this quick tip, we’ll take a look at some steps you can take to track down this mosquito and squash it good.
Introduction
This piece is the first followup to the more general “Fixing Bugs in AS3” tutorial. If you wish to better understand some of the techniques in this tip, you may wish to read that in full first.
Step 1: Understand the Error
It’s too bad that Adobe doesn’t (or can’t) provide more information about the root of this error. First of all, it’s rather obtusely worded (much like all of their errors, but this more so than most):
TypeError: Error #1009: Cannot access a property or method of a null object reference
Let’s try to put this in everyday terms. Error 1009 means that you’ve tried to do something with a variable that you assume has a value, but really does not. Flash doesn’t like that. You wouldn’t like it either; imagine you had a glass that you assumed was full of the tasty beverage of your choice, but really was empty. You grab the glass, expecting a refreshing swig, but you feel the disappointing weight of an empty glass instead. That’s your own personal Error 1009.
In ActionScript, if you do this:
var s:String; trace(s.toUpperCase());
Flash will bum hard (a technical term for “produce an error”) when you run the code. The variable s
may have been declared, but its value is null
(we never set the value, just declared the variable), so calling the toUpperCase
method on it is troublesome.
To be clear, because s
is declared as a String
, the compiler takes no issue with the code: there is a variable called s
, it’s a String
, and toUpperCase
is a valid method to call on String
s. The error we get is a run-time error, which means we only get it when we run the SWF. Only when the logic is performed do we now get to see how this turns out.
Step 2: Permit Debugging
[...]
Quick Tip: How to Debug an AS3 Error #1009
No comments:
Post a Comment