3 Replies Latest reply on Jan 4, 2008 6:31 AM by StockTraders

    1120: Access of undefined property ....


      this error is really driving me crazy and in my opinion it is a bug of the compiler:

      import mx.collections.*;
      //I define the expenses Array collection to use with the PieChart
      public var expenses:ArrayCollection = new ArrayCollection([
      {Expense:"Lovely", Amount:10},
      {Expense:"Nasty", Amount:20}
      public var lovely:int=1;
      public var nasty:int=1;
      //Now as example I decided to use the setItemAt method to change the first row and works very well
      private var testvariable1:Object=expenses.setItemAt({Expense:"Lovely", Amount:lovely},0);
      //Now I do the same with a completely equivalent statement
      //I start to define the variable
      private var apollo:Object;
      //then I use the defined variable in the setItemAt invokation
      apollo=expenses.setItemAt({Expense:"Nasty", Amount:nasty},1);
      this provokes 3 errors on the same line:
      1120: Access of undefined property apollo.
      1120: Access of undefined property expenses
      1120: Access of undefined property nasty.

      What is the real difference between:
      private var apollo:Object=expenses.setItemAt({Expense:"Nasty", Amount:nasty},1);
      //That works very well

      private var apollo:Object;
      apollo=expenses.setItemAt({Expense:"Nasty", Amount:nasty},1);
      //That provokes 3 different errors?

      I tried this other trick as well:
      private var testvariable1:Object=expenses.setItemAt({Expense:"Lovely", Amount:lovely},0); //works
      testvariable1=expenses.setItemAt({Expense:"Nasty", Amount:nasty},1); //error!

      What is the explanation?
      Kind regards,
      Marco S. Zuppone
        • 1. Re: 1120: Access of undefined property ....
          Hi Marco, what is the location of the statement that's causing the compiler errors? I.e., is the code in your example inside a <mx:Script><![CDATA[ block? If so, you can have variable declarations with assignments as in your example, but you can't have stand-alone statements and expressions; they have to be enclosed in a function. If your example is part of a <mx:Script><![CDATA[ block, you can fix the problem by doing something like this:

          private function init() : void {
          apollo = expenses.setItemAt({Expense:"Nasty", Amount:nasty},1);

          • 2. Re: 1120: Access of undefined property ....
            Bruce gave an excellent answer. And to expand on it, it looks like what you're trying to do is write code akin to how JavaScript works in an HTML page, where anything outside of a function executes as the page is initially rendering. To get the same result in Flex, you need to move that code inside of a function, as Bruce described, and then call that function from an event handler that fires as the app or component starts or finishes rendering (depending on what you want to do). For example, you could add creationComplete="init()" to the top level MXML tag that your snippet is inside of (such as mx:Application) to call Bruce's function and achieve the desired result.

            The reason
            private var myvar:Object = something();
            works is because you can call methods in the course of assigning an initial value to a variable declaration. To call something() outside of the context of a declaration, you should do so inside a function.

            Hope that helps,
            • 3. Re: 1120: Access of undefined property ....
              StockTraders Level 1

              thanks a lot for your answers!
              I think it is a bit odd rule imposed by this language :-)
              Thanks a lot!!