What happens when an ArrayCollection isn’t an Array?

March 27th, 2007 . by polyGeek

In 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.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

8 Responses to “What happens when an ArrayCollection isn’t an Array?”

  1. comment number 1 by: Alejandro

    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 ;)

  2. comment number 2 by: polyGeek

    @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.

  3. comment number 3 by: Ivan Rojas

    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;

  4. comment number 4 by: polyGeek

    @Ivan, nice, simple solution. Thanks for pointing that out.

  5. comment number 5 by: Biffer

    Thanks. I was too struggling with this one for hours until i came across this. Too kind.

  6. comment number 6 by: polyGeek

    @Biffer, that’s why I post these solutions here. Because I spent hours and hours saying “WTF?” to myself before figuring it out.

  7. comment number 7 by: Tanveer

    Thanks budddy. Keep up the good work.

  8. comment number 8 by: Sugo

    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

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites