5 Replies Latest reply on Jan 26, 2008 12:20 AM by AS3dev

    1120: Access of undefined Property

    hugovz
      I'm trying to create a simple Class and I keep getting the error 1120: Access of undefined property path when I try to access a default property. Any ideas?

      1) here's my simple class (it's in a components folder I created in src/components):
      =======================
      package components
      {
      public class MyClass
      {


      // ------- Properties -------
      public var myproperty:String = "this is my property";

      }

      }

      ============================



      2) Here's how I'm trying to access the class from an mxml application file in the src folder
      =====================================
      <mx:Script>
      <![CDATA[
      import components.MyClass;
      public var test:MyClass= new MyClass() ;
      trace(test.myproperty)
      ]]>
      </mx:Script>
      ===================================

      The error message I keep getting is:
      1120:Access of undefined property test.

      Why is it saying test is undefiined??

      Any help would be tremendously appreciated!
      Thanks!

        • 1. 1120: Access of undefined Property
          markus.rossler
          two ideas (totally different things, but my second idea comes closer to the error i think you thought about)
          a) did you add the classpath src/classes?
          b) you are defining the "myproperty"-String as "private".
          there are 2 ways to make it visible:
          b1) a getter/setter-method like this:
          public function get myProperty():String { return myproperty; }
          public function set myProperty(pMyProperty):void { myproperty = pMyProperty; }
          b2) you define the property public

          hope it helps.
          • 2. 1120: Access of undefined Property
            hugovz Level 1
            Thanks for your help Markus.

            So, I changed my code to

            public var myproperty:String

            (edited this in my above post)

            I also tried adding a getter. But neither of these things work, and I still get the same error. How do I add the classpath? Can you provide instructions? I thought that was something you only had to do with Flash, not Flex (as I'm trying to use this class in a Fex Builder 3 application).

            Also, it seems weird that its saying the undefined property is "test" and not "myproperty". But, I clearly defined "test" as you see in my code above in the mxml script tag.


            Thanks!
            • 3. 1120: Access of undefined Property
              hugovz Level 1
              Well, I think I figured this out, but I don't really understand why it works this way.

              The code is all the same as above, the only difference is that I created a function and now access the class inside of that function after the application creation is complete...it is invoked on creationComplete of mx:Application.

              So what I don't understand is, why is it that you can't access the property of a custom class until the entire application has completely loaded. Can someone please explain? I'm still learning Flex and would really like to understand better.

              Anyway, here are the details of what I did:

              1) Create the class and store it in src/components as MyClass.as
              =========scrc/components/MyClass.as===========
              package components
              {
              public class MyClass
              {
              // ------- Properties -------
              public var myproperty:String = "this is my property";
              }
              }
              ==============================

              2) In Main.mxml import the class, instantiate the class, then access properties of class after application creation is complete by creating an init function and calling that function on creationComplete:
              =========scrc/Main.mxml===========
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
              <mx:Script>
              <![CDATA[
              //import class
              import components.MyClass
              //instantiate class
              public var test:MyClass = new MyClass();


              private function init():void{
              //now you can access property of class AFTER application creation is complete
              trace (test.myproperty);
              }
              ]]>
              </mx:Script>
              </mx:Application>

              ==============================================

              I know this all seems basic, but I thought I'd list out the details in case some one else gets stuck... I only hope someone can explain to me why (in clear details) that the application must be completed first prior to accessing the property of a custom class, yet the class can be instantiated outside of the init function and prior to creationComplete??
              • 4. Re: 1120: Access of undefined Property
                theklue
                The problem here is that, when you define a variable or a method inside a Script tag, the compiler translates this into static properties/methods. The code above is equal to this:

                package {
                public class Temp {
                public var myVar:String = "temp";
                trace (myVar);
                }
                }

                You cannot call methods directly inside a class (this is not like the Flash good old days... :) )

                HTH,

                Alberto
                • 5. Re: 1120: Access of undefined Property
                  AS3dev
                  hugovz,

                  It isn't about initialization. It is about where the compiler expects code to be. In Flash, the compiler read code like a script, and you could put anything you wanted anywhere. In Flex, the compiler expects strict OOP, where a class can only be defined in a package, a function can only be defined in a class, a variable can only be defined in a class or in a function, and function calls and variable access can only happen inside a function. The error was essentially, "You can't make a function call outside of a function.", but showed up as "test is undefined" because the compiler isn't intelligent enough to figure that out because it got stuck on trying to figure out if it was a static member, as theklue mentioned.