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
I think what the livedoc is trying to say is that if there is an object at that index, it will be returned, otherwise null is returned. But if you try to access things outside of the valid range, of course you should get an exception. It would be bad if the framework silently lets you do that without alarming you of your bug.
Hmm, maybe, but it still sounds kinda shady to me
Actually, I have the same problem, if you try and test it against null it passes the test, and never returns null.
(myArrayCollection.getItemAt(invalidLocation) == null) never returns false.
..but you do get a RangeError, right?
We all know that Adobe Flex still have lots of errors that are quite weird that we don’t get on other tools. Maybe we give Flex some more time. Maybe on Adobe Flex 5 some of the “weird” issues would be solve.
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.