Variable Undefined? I think not!

public class FightGame extends MovieClip
	{
		private var _character:Character;
		private var antagonist:MovieClip;
		private var loadit:Loader;
		
		public var charList:XML;                                          // This is completely defined!!!
		public var charLoader:URLLoader = new URLLoader();
		
		public function FightGame()
		{
			
			charLoader.load(new URLRequest("charlist/charlist.xml"));

			charLoader.addEventListener(Event.COMPLETE, processXML);

			function processXML(e:Event):void
			{
				charList = new XML(e.target.data);
			}
		}
		
		trace(charList.CHARACTER.length());   // Error occurs at this line
		
	}
}

Says 1120: Access of undefined Property charList.

This is driving me nuts! Please help.

You’re using an instance variable inside the static initializer, which is code that runs when your class is first loaded before any particular instance is made. So move that line into the constructor or into an instance method.

1 Like

Also, it won’t be defined until the XML has loaded and the event handler called.

2 Likes

Ah that’s true; I was just looking at the immediate problem.

2 Likes