6 Replies Latest reply on Apr 8, 2011 12:50 PM by JMResler

    Failed ArrayCollection getItemAt

    JMResler

      Hi,

          I'm making a web service call that returns a bunch of user objects. The result handler indicates one user is returned and in the debugger I can see the user at index 0 of the array collection. I can also see the user populated in the AdvancedDataGrid object view. When I attempt to get this user, null is always returned from getItemAt(0) in the array collection.. I've tried to trace the code to see what's going on with the user but I get a null exception when I try to trace the values as follows:

       

      protected function instructorAdvancedDGItemClickHandler(event:ListEvent):void

      {

      var index:int = event.rowIndex;

      trace(index);

      trace(instructors.length);

      if (instructors.length > 0 && index < instructors.length)

      {

      selectedInstructor = instructors.getItemAt(index) as User;

       

      if (selectedInstructor != null)

      {

      trace(selectedInstructor.Name);

      trace(selectedInstructor.Token);

      trace(selectedInstructor.Id);

      trace(selectedInstructor.Token)

      trace(selectedInstructor.UserType);

      } else {

      trace("array length is = " +

        instructors.length +

        " selectedInstructor is = " +

        (selectedInstructor == null ? " null " : " not null! "));

      }

      }

      }

        • 1. Re: Failed ArrayCollection getItemAt
          EvyatarBH Level 3

          As a workaround that might help you pinpoint the problem I would try the following -

          ArrayCollection(instructorAdvancedDG.dataProvider).getItemAt(index)

           

          You might find out it's not the same data object...

           

          On second thought, it would probably make more sense to always go through the event's target (or target's owner) dataProvider.

          That way the function would be more reuseable (even if it doesn't have to be currently).

          • 2. Re: Failed ArrayCollection getItemAt
            UbuntuPenguin Level 4

            Are you sure index is equal to zero ?  If I remember correctly, the row index is the , well, row index of the rendered item.  So if you scroll up 3 renderers and click the top one, the row index will be 0, but the actual index in the collection will be 3.  I might be wrong about this, but you could always do something like this to put the issue to rest.

             

            var selectedObject:Object = event.currentTarget.selectedItem;

             

            Try that and see if you have any more problems.

            • 3. Re: Failed ArrayCollection getItemAt
              DreamTheImpossibleDream Level 1

              Perhaps check that the elements in the ArrayCollection are actually User objects.  The statement:

               

              instructors.getItemAt(index) as User

               

              will return null if that item is not actually a User object.  I believe by default the WebService returns objects that are instances of ObjectProxy, which have all the same properties as your actual object, but they may not be actual "User" instances in your case.

              • 4. Re: Failed ArrayCollection getItemAt
                JMResler Level 1

                Hi,

                   I traced the object directly from the ArrayCollection and the print statement was [object User].

                I also confirmed I'm getting it from the right index. This is maddening.

                • 5. Re: Failed ArrayCollection getItemAt
                  Flex harUI Adobe Employee

                  What is the exact text and stacktrace of the error?

                  • 6. Re: Failed ArrayCollection getItemAt
                    JMResler Level 1

                    Thanks for your help. I had to dig but someone on this thread answered the problem, it just wasn't readily apparent. The error was a type coercion error in reality but the array collection returning null for bad casts masked the problem. I had to write my own converter. The objects are identical but from different web services so the packages are different. If you have a better way, I am all ears. Thanks.

                     

                    Sent from my iPhone