« This is the end | Main | Flex 2 Runtime Error 1009 and Runtime Modules »

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.

Tags: actionscript as3 flex programming


Thanks for this tip! I was struggling with the same problem and it seems to work now. Enrique.
The most common cause is not declaring a class which is linked to a movie clip as public. Here is more on the issue.
Thanks Daniel! It was indeed very helpful.
Thankyou Enrique Rodriguez ! That's something that people new to as3 OOP maynot have noticed. It's often the simple things! Classes need to be public if they are providing the base for a symbol in the library. Cheers! Hoecus-Poke-us
Error just started popping up, was fixed by using your tips :) Thanks!
Thanks for that - that helped!
Damn, you're right! I was spending days around this problem. This is a bug, because the import statements should clarify which classes are known in the app.
Few weeks ago I was desperately looking for how to dynamically instantiate classes in AS3 but I was unable to find any resource on this, but fortunately today i was searching for 'flash Error #1065:', and I found what I was looking for weeks. Thanks a TON !

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:

var request:URLRequest = new URLRequest( "RuntimeLoaderAssets.swf" );
var context:LoaderContext = new LoaderContext(
  false, ApplicationDomain.currentDomain);
loader.contentLoaderInfo.addEventListener( Event.INIT, loaderInit );
loader.load( request, context );

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).

var assetClass:Class = ApplicationDomain.currentDomain.getDefinition(fullExternalClassName) as Class;
var background:Sprite = new assetClass(); // loaded asset is a subclass of Sprite
addChildAt(background, 0);

If you prefer, you can instead load the external swf into a appdomain which is a child of the parent's appdomain:

var context:LoaderContext = new LoaderContext(
  false, new ApplicationDomain( ApplicationDomain.currentDomain ) );
loader.load( request, context );

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)

I was banging my head into my granite counter top when I suddenly started getting this error... but I ran the "clean" function under project and it immediately rectified the situation. Phew. Let's hope it doesn't happen again.
This error was absolutely driving me nuts. Adding a variable reference did the trick! While finding out that I had to something that asinine made me nearly tear my hair out, I'm so so glad to have the problem fixed. Thanks so much! Very helpful tip.
Thank you thank you thank you. That would have probably taken hours of debugging to rectify this error. Keep up the good work!
Thanks! I was puzzled by this exact same problem until I saw this. Good post! If you don't want to make a variable for each datatype, there is another solution. Somewhere in your app you can have an array that has all the class variables in it. The class gets imported and is referenced, but you don't have to make a ton of properties if you're dynamically getting a lot of classes. For example: var dynamicClasses:Array = [ MyDynamicClass, MyOtherDynamicClass ]; You just have to make sure the import statements get added.
thanks so much..what a horrible bug..you saved our group project!
Thanks man,You saved my day :)

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)