4 Replies Latest reply on Apr 1, 2008 9:14 AM by Ansury

    ArrayCollection.getItemAt()

    XXIlladelphia Level 1
      public function getItemAt(index:int, prefetch:int = 0):Object
      Gets the item at the specified index.

      Returns
      Object — The item at that index, or null if there is none.


      By definition, this is supposed to return null if the item does not exist. When I test (ArrayCollection.getItemAt(invalidIndex) == null) it never returns true. However, if I trace that same index, it will break. How can I know if there is an item at that index or not?
        • 1. ArrayCollection.getItemAt()
          levancho Level 3
          it throws exeption if index is out of bounds :

          public function getItemAt(index:int, prefetch:int=0):Object
          {
          if (index < 0 || index >= length)
          throw new RangeError(StringUtil.substitute(resourceOutOfBounds, index));

          return source[index];
          }


          but, if you wrap your code inside try {} catch blocks and ignore the exception I bet then return value is null :)
          • 2. Re: ArrayCollection.getItemAt()
            Ansury Level 3
            Found this thread after finding the same thing in ArrayCollection (or ListCollectionView anyway).

            But I disagree with the conclusion...

            The language reference (for 3) says this:

            getItemAt
            ...
            Gets the item at the specified index.
            ...
            Returns
            Object — The item at that index, or null if there is none.


            It can't return null if it's throwing that exception, it's one or the other. This is either a BUG or an ERROR in the documentation. =(

            Catching the exception is one way to check to see if it's there, but I wouldn't do that, I'd check the length of the collection first. I always try to keep exceptions as exceptional as possible... so when I see one, I know something's wrong.
            • 3. Re: ArrayCollection.getItemAt()
              ntsiii Level 3
              On thing that may be complicating this is that ArrayCollection can be "sparse". That is there may be an item at 0, and one at 2, but nothing at 1. So an index of 1 would be valid and not error, but getItemAt(1) would return null.

              Tracy
              • 4. Re: ArrayCollection.getItemAt()
                Ansury Level 3
                But the docs don't specify anything like that, and there's no mention of an exception being thrown in any case (as Java docs would have mentioned).

                = mistake somewhere as far as I'm concerned. It's probably more of a documentation error than a bug but it should still be clarified, it's obviously wasting some people's time needlessly when encountered.