Search code examples
apache-flexblazeds

Null values when reading remote object from java POJO using BlazeDS and Flash Builder


I'm not getting any errors when I run the following files, but the data returned from the Java POJO object coming from the trace() statements below is:

[object ComputerInfo]
null
[object ComputerInfo]
null

and I'm out of ideas how to debug this. I've tried to adapt the code taken from here.

My client files are as follows. My mxml file is:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script><![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;
        import mx.rpc.events.FaultEvent;

        private var reqId1:int = 0;

        public var dataReadFromDB:ComputerInfo = new ComputerInfo;

        private function readFaultHandler( event:FaultEvent ):void 
        {
            Alert.show( event.fault.faultString, "Error reading data" ); 
        }

        private function readResultHandler( event:ResultEvent ):void
        { 
            dataReadFromDB.javaVersion = event.result.javaVersion; 
            dataReadFromDB.javaVendor = event.result.javaVendor; 
            dataReadFromDB.os = event.result.os; 
            dataReadFromDB.osVersion = event.result.osVersion; 
            dataReadFromDB.requestId = event.result.requestId; 
            trace(dataReadFromDB);
            trace(dataReadFromDB.javaVersion); 
            trace(event.result);
            trace(event.result.javaVersion);
        } 

    ]]></fx:Script>

    <fx:Declarations> 
        <mx:RemoteObject 
            id="ro" 
            destination="myDestination"
            showBusyCursor="true">
            <mx:method name="readData" 
                      result="readResultHandler(event);"  
                      fault="readFaultHandler(event);"/>
        </mx:RemoteObject>
    </fx:Declarations>

    <mx:Panel width="476" height="281" 
              layout="absolute" title="BlazeDS Example" 
              cornerRadius="0" backgroundColor="#ffffff">
        <s:Label x="46" y="59" text="Click to Read Data" />
        <s:Button x="200" y="59"  label="Read Now"   click="ro.readData( reqId1++ )"/>
    </mx:Panel> 
</s:Application>

ComputerInfo.as file:

package {
    [RemoteClass(alias="ComputerInfo")]
     public class ComputerInfo {
        public var javaVersion:String;
        public var javaVendor:String;
        public var os:String;
        public var osVersion:String;
        public var requestId:String;
    }
}

My java files are:

public class MyClass {
    public ComputerInfo readData( String requestId ) {
            ComputerInfo computerInfo = new ComputerInfo();
            computerInfo.javaVersion = "java.vm.version";
            computerInfo.javaVendor = "java.vm.vendor";
            computerInfo.os = "osname";
            computerInfo.osVersion = "os.version";
            computerInfo.requestId = "requestId";
            return computerInfo;
    }
}

and a separate file called ComputerInfo.java:

public class ComputerInfo {
    String javaVersion;
    String javaVendor;
    String os;
    String osVersion;
    String requestId;
}

Inside remoting-config.xml:

<destination id="myDestination">
            <properties>
                    <source>myClass</source>
            </properties>
</destination>

Solution

  • You seem to be missing the [RemoteClass] meta tags in your data transfer object (ComputerInfo.as):

    package {
        [RemoteClass(alias="com.myjavapackage.ComputerInfo")]
        public class ComputerInfo { ... }
    }
    

    (where you'd have to replace "com.myjavapackage" with the appropriate values; the package was not mentioned in your code samples)

    If the tags are missing, Flex won't be able to deserialize the data into the appropriate type, and you get an object of type "ObjectProxy" instead.

    You can also remove your own copying of the result values - event.result will already be of type ComputerInfo, if everything is registered correctly.

    EDIT

    Also, your the field variables in your Java class should be public, or there should be public getter and setter methods.

    For more info, see the Adobe Doc. The relevant section is "Explicitly mapping ActionScript and Java objects".