views:

2042

answers:

4

hi,

im having trouble with the result of a webservice call. When the result comes in and kicks off the resultHandler function i set a break point so i can examine the result. I can see that there are 0 records in the array collection however i can see content so im assuming that the zero is just referring to the first index of the array

the problem happens when i try assign the value to an array collection as follows;

public function resultHandler(event:ResultEvent):void{
    var result:ArrayCollection = event.result as ArrayCollection;

the result of this operation is a result var with the value of null. Can anyone explain what could be happening here? thanks a lot

another thing i just noticed is that the result type is mx.utils.ObjectProxy, im expecting an array

+1  A: 

If the webservice returns just one element, it will be deserialized as ObjectProxy. You will have to manually convert it into an array.

I'd normally do this after a WS call:

if (event.result is ArrayCollection) {
    result = event.result;
}
else {
    result = new ArrayCollection([event.result]);
}
Chetan Sastry
+1  A: 

Chetan is right -- the cast operation to ArrayCollection is failing, because the source object is not an ArrayCollection. Try this instead:

public function resultHandler(event:ResultEvent):void
{
    var ac:ArrayCollection = new ArrayCollection([event.result])
    // ...
}

The "as" operator will return null in situations where an exception would occur at runtime -- in your case, casting from ObjectProxy to ArrayCollection. If instead you pass event.result as the sole member of an array (by surrounding it with []), your ArrayCollection will be constructed properly, and you'll be able to retrieve the object normally:

var o:Object = ac.getItemAt(0) as Object;
trace(o.yourObjectProperty.toString());

Hope it helps!

Christian Nunciato
A: 

0 records in the array is the length of the array, that actually means 0. If you have something in index 0 of an array, that array has a length of at least 1. It looks like you aren't getting any data back, not even and empty array collection.

Ryan Guill
A: 

Thanks for your explanations.. It works nice :)

binnur