3 Replies Latest reply on Jun 9, 2010 1:43 AM by johnjoe

    RemoteObject not casting to correct type

    johnjoe Level 1

      Hello,

       

      I have a Flex/Java project that has 47 Java DTO's and 47 ActionScript VO's. All but one of these classed can be passed between flex and java with out a problem. I can't see any noticeable difference between the problem classes and the others. I am getting this error: flex.messaging.io.amf.ASObject cannot be cast to com.fujitsu.ad.services.db.dto.UserRole. I notice during debug that objects on the java side that have been recieved from flex are of type ASObject and on the flex side objects that have been pass from java have type Object. I have include both classe below in the hope that someone migt be able to see something obvious.

       

      Thanks,

       

      JJ

       

      package

       

       

      com.fujitsu.ad.services.db.vo

      {

       

      import mx.utils.ObjectUtil;

       

      [

      Bindable]

      [RemoteClass(alias=

      "com.fujitsu.ad.services.db.dto.UserRole")]

       

      public class UserRole

      {

       

      public var id:Number;

       

       

      public var name:String;

       

       

      public var description:String;

       

       

       

      public function UserRole()

      {

      }

       

       

      public function clone() : UserRole

      {

       

      return ObjectUtil.copy( this ) as Role;

      }

       

      }

      }

       

      /**
      * Copyright (c) 2010 Fujitsu Services Ltd.
      */
      package com.fujitsu.ad.services.db.dto;

      /**
      * The Class Application.
      */
      public class UserRole implements java.io.Serializable {

          /**
           * serialVersionUID.
           */
          private static final long serialVersionUID = 1L;

          /** The id. */
          private long id;

          /** The name. */
          private String name;

          /** The description. */
          private String description;

          /**
           * Instantiates a new application.
           */
          public UserRole() {
          }

          /**
           * Gets the id.
           *
           * @return the id
           */
          public long getId() {
              return this.id;
          }

          /**
           * Sets the id.
           *
           * @param id the new id
           */
          public void setId(long id) {
              this.id = id;
          }

          /**
           * Gets the name.
           *
           * @return the name
           */
          public String getName() {
              return this.name;
          }

          /**
           * Sets the name.
           *
           * @param name the new name
           */
          public void setName(String name) {
              this.name = name;
          }

          /**
           * Gets the description.
           *
           * @return the description
           */
          public String getDescription() {
              return description;
          }

          /**
           * Sets the description.
           *
           * @param description the new description
           */
          public void setDescription(String description) {
              this.description = description;
          }

          /**
           * @see java.lang.Object#hashCode().
           * @return the hash code
           */
          @Override
          public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + (int) (id ^ (id >>> 32));
              return result;
          }

          /**
           * @see java.lang.Object#equals(java.lang.Object).
           * @param obj
           * @return true or false
           */
          @Override
          public boolean equals(Object obj) {
              if (this == obj) {
                  return true;
              }

              if (obj == null) {
                  return false;
              }

              if (getClass() != obj.getClass()) {
                  return false;
              }

              final UserRole other = (UserRole) obj;

              if (id != other.id) {
                  return false;
              }
              return true;
          }

      }

        • 1. Re: RemoteObject not casting to correct type
          Ansury Level 3

          Sometimes you run into this because the class was not added to your swf when it was compiled (because there were no references to it in the code!)

           

          If you're sure everything looks correct code wise, try adding a reference to the class (in Flex) that's not being strongly typed something like this:

           

          var role:UserRole = object as UserRole;  (with the usual import statement, actually just the import may even be enough)

           

          (Make sure to place this in some class that is definitely being used, not the VO itself.)

          • 2. Re: RemoteObject not casting to correct type
            Karl_Sigiscar_1971 Level 3

            That's correct. As stated in the documentation:

             

            In the ActionScript class, you use the [RemoteClass(alias=" ")]

            metadata tag to create an ActionScript object that maps directly to the Java object. The ActionScript class to which data is converted must be used or referenced in the MXML file for it to be linked into the SWF file and available at run time. A good way to do this is by casting the result object, as the following example shows:

             

            var result:MyClass = MyClass(event.result);

            The class itself should use strongly typed references so that its dependencies are also linked.

            • 3. Re: RemoteObject not casting to correct type
              johnjoe Level 1

              Thank you. Problem solved