7 Replies Latest reply on Dec 15, 2006 2:44 AM by dfrodrig

    variable instantiation not working

    dfrodrig
      Hello all,

      I have the most weird problem: Inside a custom component based on list, I'm getting an arrayCollection from a RemoteObject, and after I want to create a new array where I set (in the client) which elements are, or are not selected). In the first iteration of coding (just trying to create a new array without any problems), I instantiate a variable, but the reference is still null!!!

      So, rewind, and focus on the single problem: I create a variable, using new FollowUpCardActionVO(), and still I get a null reference.

      More bizarre is that, actually, I can access the members in the created object, but when trying to add it to an array collection, I get the error: "TypeError: Error #1009: Cannot access a property or method of a null object reference."


      The buggy part of the code follows:

      var tmpSpActionTypesList:ArrayCollection = event.result as ArrayCollection;
      var tmpFollowUpCardActionVO:FollowUpCardActionVO = new FollowUpCardActionVO();
      for (var i:int=0; i < tmpSpActionTypesList.length; i++) {
      Debug.println("Adding new FollowUpCardActionVO() ["+i+"]");
      Debug.println("Setting ActionId: " + tmpSpActionTypesList .actionId);
      Debug.println("Setting ActionToken: " + tmpSpActionTypesList
      .actionToken);
      tmpFollowUpCardActionVO.actionId = tmpSpActionTypesList .actionId;
      tmpFollowUpCardActionVO.actionToken = tmpSpActionTypesList
      .actionToken;
      tmpFollowUpCardActionVO.selectedCheckBox = false;

      Debug.println("Item created: " + tmpFollowUpCardActionVO.toString());
      spActionTypesList.addItem(tmpFollowUpCardActionVO);
      }

      When addingItem, the error is displayed, if I instantiate a new object at every iteration of for same thing happens. the console output is as follows :

      -------------------------------------------------------------------------
      [AS DEBUG] Adding new FollowUpCardActionVO() [0]
      [AS DEBUG] Setting ActionId: 16
      [AS DEBUG] Setting ActionToken: Help user
      [AS DEBUG] Item created: null
      :: actionid 16
      :: actionTokenHelp user
      :: selectedCheckBoxfalse



      and the VO class:

      ----------------------------------------
      package valueObject.followUpCard
      {
      import mx.collections.ArrayCollection;


      /**
      * Manage Data to transfert beetwwen Java and AS.
      * Contains Card Status elements
      * @author dro
      *
      */
      public class FollowUpCardActionVO
      {
      /**
      * Action Type Identifier
      */
      public var actionId:int;

      /**
      * Action Token
      */
      public var actionToken:String;

      /**
      * flag to be used in the card action type listBox
      */
      public var selectedCheckBox:Boolean=false;

      /**
      * Constructor
      */
      public function FollowUpCardActionVO() {
      super();
      }

      public function toString():String {
      var toString:String;
      toString += "\n" + ":: actionid " + actionId;
      toString += "\n" + ":: actionToken" + actionToken;
      toString += "\n" + ":: selectedCheckBox" + selectedCheckBox;
      return toString;
      }

      }
      }


      Thanks, any help would be appreciated... I can work around it, but also would like to figure out what's happening!
        • 1. Re: variable instantiation not working
          dfrodrig Level 1
          PS: This is inside the script part of my mxml component.
          • 2. Re: variable instantiation not working
            ntsiii Level 3
            You have instantiated "tmpSpActionTypesList"

            but not "spActionTypesList."

            Also, have you verified event.result is not null?

            Tracy
            • 3. Re: variable instantiation not working
              dfrodrig Level 1
              hello.

              event.result is not null. tmpSpActionTypesList is not null, as i can access "lenght" property, as well as the result itself (
              tmpSpActionTypesList\[i\].actionId / tmpSpActionTypesList\[i\].actionToken)... you may see it from the debug!

              It's really bizarre, and the problem is in the actual instantiation of FollowUpCardActionVO not working properly (it's null, but still allows access to it's members :S)

              PS: \[.i\] is not being properly displayed, but I'm accessing the array items properly...
              • 4. Re: variable instantiation not working
                ntsiii Level 3
                Hmm, I see. Have you imported FollowUpCardActionVO?

                Can you instantiate this class at all?

                Tracy
                • 5. variable instantiation not working
                  FlightGuy Level 1
                  I note that your toString method on FollowUpCardActionVO is declaring a new string toString, but not setting a value. You then proceed to append strings to it. In other words the first string assignment is:
                  toString += "\n" + ":: actionid " + actionId;
                  which is analogous to
                  toString = null + "\n" + ...
                  I expect this is why your toString is returning null in your debug message. It's also hazardous to shadow your toString method by declaring a local variable by the same name, though that won't confuse the compiler - just exposes you to errors.

                  The reason it appears you're getting the error on the add is that your collection has never been initialized - as Tracy noted in her first response, you don't initialize spActionTypesList.

                  If tmpFollowUpCardActionVO were really null, you wouldn't be able to invoke its toString method :)
                  • 6. Re: variable instantiation not working
                    ntsiii Level 3
                    Good catch!
                    Tracy
                    • 7. Re: variable instantiation not working
                      dfrodrig Level 1
                      Thanks a lot!! It was indeed spActionTypesList which was not initializated... :S

                      Sorry for replying this long after, but I was focusing on delivering other components for a while, so only now could get back to this one.

                      Lessons to learn: You can't add a item to an ArrayCollection which is not initialized! Pretty obvious, and this can't be excused by "begginers mistake"... I don't really know what crossed my mind!

                      Interesting enough to notice that toString is initialized as "null"... :S

                      btw, I replaced toString by stringToReturn... it definitely is an excellent suggestion!


                      thanks guys, I'll come back surely with obvious mistakes ;-)