Skip to content

Get Line Numbers on Runtime Errors in Flash!

So, this is something that’s really simple and I really wish I’d known about this years ago – would have saved me much time. Ever find yourself working on a project, and it’s 3am, and you’re exhausted but just want to kill this one bug, but WTF it’s a runtime error of some sort and you have no idea what’s causing it, only that something somewhere is null when it’s not supposed to be? You know, "TypeError: Error #1009: Cannot access a property or method of a null object reference..." with a stack trace?

I mean, yeah, you get the stack trace so you have a general idea of where the problem is – but if you’re dealing with a bunch of big value objects and you have 50 variables, any one of which may or may not be null for any number of reasons, don’t you just find yourself screaming in frustration that Flash won’t give you a line number to go with the error? …Is that just me?

[Edit: now, understand, your code should really be architected to fail early and fail often to avoid these kinds of clusterfucks - but sometimes that's not an option, and you just get handed a big steaming mess and hey, you know the drill.]

Let me show you what I mean. Create a new AS3 project in Flash, and setup your document class to look like this:

	import flash.display.MovieClip;
	public class MyDocumentClass extends MovieClip
		public var myVariable:MovieClip;
		public function MyDocumentClass()
the Permit Debugging checkbox

Check this to get detailed runtime errors - but remember to uncheck it when you're done!

Now try to run your Flash file. You’ll get a runtime error, because myVariable is never instantiated and so it’s impossible to access myProperty on it. But the runtime error info you get doesn’t give you a line number – so imagine if you have a lot of variables in the function that’s throwing the error. It’s a huge pain, because you have to comment them out one at a time to figure out which one is accidentally null. Pain in the ass, right?

Anyway, turns out it’s really easy to enable additional details in your Flash Runtime Errors. Go into your publish settings and check the “Permit Debugging” checkbox. Now try to compile again – if you look, you’ll see that your runtime null reference error now tells you exactly which file in your source code has the error, as well as exactly which line number the error is taking place on. Nifty, eh?

Now, one thing you’ll want to be aware of: “Permit Debugging” defaults to off for a reason. Anyone looking at your .swf and trying to reverse-engineer your code now has a LOT more information than they normally would. NEVER go live with debugging turned on – you’re leaving yourself open to hackers etc to come in and use your files in ways that you never intended.

That is all! I hope it helped!

{ 1 } Comments

  1. mykola | August 15, 2010 at 12:12 am | Permalink

    As I understand it, Permit Debugging also unlocks some additional functionality for more advanced debugging – it lets you do things like wire your .swf up to permit access to Monster Debugger etc, or allows you to use Flash Builder’s more powerful debugger on your linked Flash Projects. All in all a useful thing to know about.

Post a Comment

Your email is never published nor shared. Required fields are marked *