Tutorial :Problems with accessing FlashVars via parameters in AS3


I keep getting compiler errors when I try to access flashVars in an AS3 class.

Here's a stripped version of the code:

package myPackage {   import flash.display.Loader;   import flash.display.LoaderInfo;   import flash.display.Sprite;    public class myClass {      public function CTrafficHandler() {          var myVar:String = LoaderInfo(this.root.loaderInfo).parameters.myFvar;}}}  

And I get a compilation error:

1119: Access of possibly undefined property root through a reference with static type source:myClass.

When I change the class row to

public class myClass extends Sprite {  

I don't get a compiler error, but I do get this in the output window:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Via the debugger (as suggested) I can see that this.root is null.

How can I solve this problem?


Your problem is that your DisplayObject has not been added to the DisplayList, at the point at which you're trying to access flash vars. The root display object is therefore null, according to your object.

You can ensure that your DisplayObject is on the stage by using the following:

package  {      import flash.display.Sprite;      import flash.events.Event;        public class MySprite extends Sprite      {          // constructor          public function MySprite()          {              super();              addEventListener( Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true );          }            private function onAddedToStage( event:Event ):void          {              removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );                var paramList:Object = LoaderInfo( this.root.loaderInfo ).parameters;              var myParam:String = paramList["myParam"];          }      }  }  



I found what the problem was. The class in question wasn't the main class used in the project, but rather a secondary class.

I've moved the code to the main class to get the parameters and after I got them, I sent them to the class constructor function.


The problem was indeed that you were attempting to access this information from a non-display object, or from outside of the document class. If you wish to access root or stage, the object that wishes to access such must be first added to the display list.

I often use flashvars for variables that are used often throughout the project. Variables like country, and language. I find that in this case it is best to catch these parameters in the document class and create public variables with said parameters as values. This will give _global style access to these variables. That all being said, you really should use global variables sparingly, especially when working on collaborative projects.


As an alternative, you could try using the mx.core.Application.application.parameters object.

From the LiveDocs page for mx.core.Application:

application : Object
[static] [read-only] A reference to the top-level application.

parameters : Object
[read-only] The parameters property returns an Object containing name-value pairs representing the parameters provided to this Application.

There are two sources of parameters: the query string of the Application's URL, and the value of the FlashVars HTML parameter (this affects only the main Application).


I think you should extend from Sprite, but be sure to initialize it first and maybe put to the stage. Try to enable debugging and see what exactly is null as exception report says.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »