ArrayCollection.getItemAt() doesn’t return null
by Ruben

Just a second ago I changed an Array property into an ArrayCollection, when I tested my project again a RangeError was thrown telling me that I was trying to access an index that was out of bounds (occuring at .getItemAt()).
Now this itself didn't surprise me alot, but what did surprise me was that I hadn't gotten this Error back when my property was still an Array.
I figured that it must be the fact that an ArrayCollection uses actual methods, like getItemAt(). To varify my assumption I looked in the LiveDocs.
Strange enough the docs say that the return value for getItemAt() is "the item at that index, or null if there is none".
I don't think I can ever trust my old friend the LiveDocs again after lying to me about returning null and then stabbing me in the back with a RangeError..
Comments (read older)
Just a question as I’m sort of running into the same problem. What DOES it return?
This is possibly old news for you. However, there is a solution to this problem. Since it’s not on here – figured I’d at least post my findings. Use Try/Catch:
function myFunction():void{
try{
myArrayCollection.getItemAt(0);
trace(‘the item is there’);
}
catch(e:RangeError){
trace(‘the item is out of bounds’); //or alert.show if you choose
}
}
Hey Carl,
quote from carl:
Well it doesn’t really return anything, the getItemAt()-method is canceled and the error is thrown. If you would set the returned value to a variable called ‘someValue’ and test the value after having tried/caught the RangeError, then you’d see that the value of someValue would still be what it was before.
wow, two years and the docs are still wrong.
They could’ve at least given us a “containsItemAt” function to match the “contains” function so we don’t need to rely on wonky exception handling.