see613
see613

Reputation: 494

Object & method by name

[ note: i'm beginner in java ]

I try to call object by name and then I try to call its method by name,

but this code throws exception java.lang.NoSuchMethodException:

Field mainClassField = mainObject.getClass()
                                 .getField( dataObject.callObject );
/* callObject is an another object */
Object callObject = mainClassField.get( mainObject );   

Method callMethod = callObject.getClass()
                      .getMethod( dataObject.callMethod ); << error on this line
callMethod.invoke(callObject, dataObject);

[ note: i pass dataObject from actionscript 3 through AMF ]

calling object code:

public class UserCallController extends Controller {

    public void getUserById(DataTransferObject dataObject) {
        Ppvchat mainClass = _inst._mainClass;

        dataObject.data.put("userData", 
                mainClass.userModel.getById( dataObject.data.getString("id") ));
    }

}

thanks.

Upvotes: 0

Views: 88

Answers (1)

AlexR
AlexR

Reputation: 115378

If NoSuchMethodException such method indeed does not exist. So, check the following.

  1. what class are you dealing with.
  2. what method are you trying to call. Double check the method name and signature.

Anyway your code seems wrong.

callObject.getClass().getMethod( dataObject.callMethod ); means that you think that method does not have arguments.

callMethod.invoke(callObject, dataObject); means that you think that this method has argument assignable from dataObject.

Upvotes: 2

Related Questions