2 Replies Latest reply on Sep 19, 2006 1:28 PM by arkcto

    Empty Array with Remote Object - HELP

    arkcto
      Hi,

      I posted a related message last week about how I am unable to retrieve an array of data from a remote object.

      I am a newbie and I've been able to call Java methods when they return a simple String. However, I am unable to get an array of Java objects.

      I have a datagrid in my application that has to be populated by calling a method of a remote object.

      The remote object (Java class looks like this).

      public class PlayerService {

      ......
      // This is the method that gets called.
      public Player[] getPlayersAsArray() {
      Player[] players = new Player[3];
      Player rahul = new Player("Rahul",,"Dravid");
      Player sachin = new Player("Sachin","Tendulkar");
      Player saurav = new Player("Saurav","Ganguly");
      players[0] = rahul;
      players[1] = sachin;
      players[2] = saurav;
      return players;
      }

      }


      I've configured the destination in my service-config.xml as
      ......
      <destination id="PlayerService">
      <properties>
      <source>rpc.playerinfo.PlayerService</source>
      <scope>application</scope>
      </properties>
      </destination>
      .....

      My mxml application code is given below

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.collections.ArrayCollection;
      import rpc.playerinfo.Player;
      import mx.utils.ArrayUtil;

      [Bindable]
      public var players:ArrayCollection;

      public function handlePlayersResult(event: ArrayCollection)
      {
      //then set the event result which is being sent into this function to the playerList
      players = event;
      }

      ]]>
      </mx:Script>

      <mx:RemoteObject id="playMe" destination="PlayerService"
      showBusyCursor="true">
      <mx:method name="getPlayersAsArray" result="handlePlayersResult(event.result as ArrayCollection)"/>
      </mx:RemoteObject>

      <mx:DataGrid dataProvider="{players}" width="100%" creationComplete="playMe.getPlayersAsArray()">
      <mx:columns>
      <mx:DataGridColumn dataField="firstName" headerText="FirstName"/>
      <mx:DataGridColumn dataField="lastName" headerText="LastName"/>
      </mx:columns>
      </mx:DataGrid>

      </mx:Application>


      When I run the application, the datagrid is blank with just the header text. I see the headers 'FirstName' and 'LastName' but the
      grid isn't populated by any values.

      Thank you so much.
        • 1. Empty Array with Remote Object - HELP
          jasowill
          Your java class is returning a native array ([]). Native arrays are translated to AS3 native arrays (Array), if you want ArrayCollection use a ArrayList or some class that implements Collection.

          In the result handler you are casting a native Array to an incompatible type ArrayCollection. Normally, you would receive a class cast exception, but, because you are using the "as" operator you are getting null as the result, which is hiding the real issue.

          To fix this change the handlePlayersResult() signature to accept an Array and change the result attribute of the remoteobject tag to use Array instead of ArrayCollection.
          • 2. Empty Array with Remote Object - HELP
            arkcto Level 1
            Got it. IT works.

            Thank you very much.