6 Replies Latest reply on Feb 11, 2011 11:10 AM by Flex harUI

    Class variables don't exist at initialize() time?

    Wrataxas Level 2

      Excerpt of Slam.mxml (default package):

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/mx"
                        xmlns:components="components.*"
                        minWidth="955" minHeight="600" width="1000" height="720"
                        initialize="Initialize()"
                        creationPolicy="auto">
           <fx:Script>
                <![CDATA[
                     protected function Initialize():void
                     {
                          Global.app = this;
                     }
                ]]>
           </fx:Script>
      </s:Application>
      
      

       

      Excerpt of Global.mxml (default package):

      <?xml version="1.0" encoding="utf-8"?>
      <fx:Object xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx">
           <fx:Script>
                <![CDATA[
                     public static var app:Slam = null;
                ]]>
           </fx:Script>
      </fx:Object>
      
      

       

      When I run the app I get an error in Initialize() because Global is null.  Global is a class name and Global.app is a class variable, a static.  How can they not exist at initialize time?  Suggestions for how to deal with this?  Thanks.

        • 1. Re: Class variables don't exist at initialize() time?
          Wrataxas Level 2

          To clarify, I would have expected all static initialization to happen before any code was run.  This isn't the case?

          • 2. Re: Class variables don't exist at initialize() time?
            Abhrodip Paul-cXMMf1 Level 2


            I donot have flex 4. Below code is in SDK3.5 and working fine. No null pointer.

             

            Test2.mxml

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                             layout="vertical"
                             initialize="Initialize()"
                             creationPolicy="auto">
                 <mx:Script>
                      <![CDATA[
                           protected function Initialize():void
                           {
                                Global.app = this;
                           }
                      ]]>
                 </mx:Script>
            </mx:Application>

             

            Global.mxml

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Object xmlns="*" xmlns:mx="http://www.adobe.com/2006/mxml">
                <mx:Script>
                      <![CDATA[
                           public static var app:Test2 = null;
                      ]]>
                 </mx:Script>
            </mx:Object>

            • 3. Re: Class variables don't exist at initialize() time?
              Wrataxas Level 2

              If I do:

                   var x:Class = Global;

               

              in my Initialize() routine, then x is null.  If I do:

                   var x:Global = new Global;

               

              this fails also.  So at initialize time this other class simply doesn't exist yet.  Is there a way to force it to exist?

               

              Alternatively, is there a better event I should be listening for?  I want something that runs before all the sub-components of the Application are created but, apparently, after classes are created.  Is there such a thing?

              • 4. Re: Class variables don't exist at initialize() time?
                Flex harUI Adobe Employee

                I ran the code you posted in the Hero prerelease.  I didn't get any errors.

                 

                If you simplify your test case to those exact files do you still have the

                problem?  Which SDK are you using?

                • 5. Re: Class variables don't exist at initialize() time?
                  Wrataxas Level 2

                  No, running just these files doesn't exhibit the issue for me.  I'm using Flex 4.1.  However, I was able to come up with a minimal set of files that does show the problem.  Slam.mxml is the same as before.  Global.mxml becomes:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <fx:Object xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
                       <fx:Script>
                            <![CDATA[
                                 public static var app:Slam = null;
                                 public static const WIDGET_COLOR:uint = 0x987654;
                                 public static var widgets:Widgets = new Widgets();
                            ]]>
                       </fx:Script>
                  </fx:Object>
                  
                  

                   

                   

                  and add an additional file, Widgets.as:

                   

                  package
                  {
                       public class Widgets
                       {
                           public function Widgets()
                           {
                                wColor = Global.WIDGET_COLOR;
                           }
                           protected var wColor:int;
                       }
                  }
                  
                  

                   

                   

                  This blows up with Global == null at the wColor initialization in Widgets.as.  I suspect there is some sort of chicken-and-egg issue going on here.  That Global isn't fully setup until it has initialized its widgets member, but initializing the widgets member needs Global fully setup.

                   

                  Moving the initialization of the widgets member to a separate Initialize function in Global, called from Slam.Initialize(), fixes the problem.

                  • 6. Re: Class variables don't exist at initialize() time?
                    Flex harUI Adobe Employee

                    Yes, that example contains a circular dependency, and you have to work

                    around it as you did.