Flex 2 Runtime Error 1065
Today while working on some code I started getting this error and stack trace:
ReferenceError: Error #1065: Variable LoadTest is not defined. at global/flash.utils::getDefinitionByName() at ... (rest of stack trace deleted)
Looking the error up in the documentation didn't help point the way to what might be going on since this had nothing to do with a "variable":
1065 Variable _ is not defined. You are using an undefined lexical reference. For example, in the following statements, the statement trace(x) generates an error because x is undefined. However, the statement trace(y) doesn't generate an error because y is defined: trace("hello world") trace(x) // x is undefined var y trace(y) // No error, y is defined.
In this case I was trying to dynamically instantiate a class by first finding the definition of that class and then newing it. Something like this:
public function createInstance(className:String):Object { var instance:Class = Class(getDefinitionByName(className)); new instance(); }
It turns out the reason why the error was being thrown was that in my Flex Application I didn't have anything which "used" the class. In other words the compiler thought that the class was left over junk and didn't include it in the compiled swf. By adding in a use of it, everything started working. As example use would be something like:
private static const loadTestRef:LoadTest = null;
If there is another way to get this same behavior, please let me know.
Comments
Posted by: Enrique Rodriguez | August 25, 2006 8:49 AM
Posted by: Andrew Paul Simmons | June 7, 2007 11:37 AM
Posted by: Bijit Bose | June 22, 2007 9:04 AM
Posted by: Ben Hoe | September 4, 2007 5:01 AM
Posted by: Bram | November 6, 2007 9:07 AM
Posted by: Judit | February 13, 2008 9:13 AM
Posted by: Chris | May 6, 2008 12:25 PM
Posted by: Mudit Tuli | August 25, 2008 2:51 AM
I had this error too, and found it to be to do with Application Domains (Flash CS3/AS3).
Since my application is loading an unknown subclass of Sprite, I wasn't able to create a 'dummy' definition as you suggest in your article. So another way to solve it is to supply the current ApplicationDomain object in the loader's context when loading the external swf:
This causes the classes defined in the external swf to be stored in the same appdomain as the parent code's appdomain.
Then, when instantiating the (externally defined) asset Class, I used the current ApplicationDomain's 'getDefinition' method rather than the flash.utils.getDefinitionByName function (which doesn't seem to understand dynamically loaded Classes).
If you prefer, you can instead load the external swf into a appdomain which is a child of the parent's appdomain:
If you do this, you'll need to call the getDefinition method on the loader.contentLoaderInfo.applicationDomain object, rather than ApplicationDomain.currentDomain
Hope this helps someone! The new ApplicationDomain concept is a little tricksy, but kinda cool. Roger Gonzalez has an illuminating blog entry about ApplicationDomains.
Thanks Daniel for the article! (sorry for the mammoth comment)
Posted by: Aidan Fraser | September 14, 2008 7:34 PM
Posted by: Ken Greenwood | January 26, 2009 8:24 PM
Posted by: Mindy | January 29, 2009 5:05 PM
Posted by: William V | February 11, 2009 5:01 PM
Posted by: Nils T | March 6, 2009 12:59 AM
Posted by: kat | April 9, 2009 8:56 PM
Posted by: Biju Varghese | October 27, 2010 4:14 AM