What happens when an ArrayCollection isn’t an Array?
March 27th, 2007 . by polyGeekIn my Seach Flex app I’m asking the Flickr API for data based on my search terms and the number of photos that I want to see. The data that comes back is returned to my event handler, via the ReseltEvent, where I can use it to create links to photos that are displayed. I’m binding that data to an ArrayCollection - named photos. Simple enough.
I have a line of code like this:
photos = event.result.rsp.photos.photo;
my photos:ArrayCollection is being set equal to the repeating element of the returned data - event.result.rsp.photos.photo.
If I ask for 3 photos to be returned then I could do something like this:
That would produce output like this:
photo ID: 431933355
photo ID: 431931375
photo ID: 431854221
But what happens if I ask for just one photo to be returned? It would be nice if I could access that data like this:
trace("photo ID: " + photos[0].id);
But I can’t. Actually, I wouldn’t even get that far because the line above where I made my photos:ArrayCollection equal to the ResultEvent would have produced an error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
Here’s the big surprise - sarcasm - an ArrayCollection can’t be set equal to something that isn’t Arrayish.
When I came upon this error it was pretty easy for me to spot because I’ve run into this before with my PXP2 class for ActionScript 2 which is a lot like the ArrayCollection. Of course when I ran into the problem the first time it took hours for me to figure out what the frak was wrong. Such is coding.
The solution is pretty simple. Here’s how I fixed it in the Seach Flickr app:
In the first if-case I’m dealing with the issue of not getting anything back. Like if you search for “iidneidntineidte” and nothing comes back.
The second if-case handles the more likely case of getting an Array back and the else handles what happens if there is just one photo returned.
So if you’re working with ArrayCollections you better make sure that you’re always getting an array of objects from where ever. Otherwise you’ll get those nasty error popups.
As an aside: this happened to me on the Zune.net media player. I set everything up using my PXP2 class to handle lots of videos and photos. Then when the production crew got it they broke it the very first day because I never tested the thing with just one photo or video. Really, the way this thing was designed it looked like there would always be lots of content. Whooops.
Lesson learned: always test with zero, one and many.












I had the same problem several times with a kind of ArrayCollection built in AS2, and at the end I did a function that forces it to be also an array of 1 element:
function checkUniqueArray(obj:Object):Void
{
if (obj.length == undefined)
{
obj[0] = obj;
obj.length = 1;
}
}
May be it is not the best solution, but it works, at least in my code ;)
@Alejandro, I like it. I’ll check and see if I can factor that into my PXP2 class and force it to return an array.
When you return something and it has just one element on it , you can assign (=) that result to an arraycollection.
first do this to do it:
private var arr:ArrayCollection;
if(result.result is ArrayCollection)
{
arr:ArrayCollection = result.result
}
else
{
var arr1:ArrayCollection;
arr1.addItem(result.result);
}
arr = arr1;
@Ivan, nice, simple solution. Thanks for pointing that out.
Thanks. I was too struggling with this one for hours until i came across this. Too kind.
@Biffer, that’s why I post these solutions here. Because I spent hours and hours saying “WTF?” to myself before figuring it out.
Thanks budddy. Keep up the good work.
Helpfull that you bloged about this problem…happened to me lots of times…Sites and tutorials dont mention this when getting results from server into Array Collection
In my code I had to put
var arr1:ArrayCollection = new ArrayCollection(); to make my code work on Ivan Rojas suggestion.
Leave a Reply