Link to home
Start Free TrialLog in
Avatar of nagunpyo
nagunpyo

asked on

How to use java.Object.invokeMethod() ?

I'm keep on getting the following error mesage
cannot reslove symbol
symbol : method invokeMethod (java.lang.String,java.lang.Ojbect[])
location: class java.lang.Ojbect
e_MLC.invokeMethod("setTName", args);

And here's part of my code that's causing the problem

Object e_MLC;
s_MLC_tName = getTName() + "_MLC";
Class cls = Class.forName(s_MLC_tName.substring(2));
Object[] args = new Object[1];
args[0] = s_MLC_tName;
e_MLC = cls.newInstance();
e_MLC.invokeMethod("setTName", args);

How am I supposed to use invokeMethod() method?
And what am I supposed to do when there's no arguments to pass?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to get Method object and invoke them
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nagunpyo
nagunpyo

ASKER

what's wrong with invokeMethod()?
invokeMethod is non-existent AFAIK
>>  what's wrong with invokeMethod()?

it's part of .NET, not Java

;-)
>>And what am I supposed to do when there's no arguments to pass?

Pass null
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>>
 >>And what am I supposed to do when there's no arguments to pass?

Pass null
>>>>

This is how:


                  Class cls = Class.forName(s_MLC_tName.substring(2));
                  Class[] argTypes = new Class[] { java.lang.String.class }; // put the correct type of arg in here
                  Method mthd = cls.getMethod( "setTName", argTypes); //find method based-on name and argument
                  mthd.invoke(cls.newInstance(), new Object[]{ null } );//invoke method here
if you want to call something like doMyJob(), you need to:

      mthd.invoke(cls.newInstance(), null);//invoke method here

The following is Doc from Method.invoke:
     If the number of formal parameters required by the underlying method is
     0, the supplied <code>args</code> array may be of length 0 or null.
Thank you guys! :-D
:-)