Hi,
I use this class to call methods of other classes:
public class FunctionHandler {
public static String execRoutine(String longName, List<String> args)
throws Exception {
String ret;
int dot = longName.lastIndexOf('.');
String className = longName.substring(0, dot);
String funcName = longName.substring(dot + 1);
printf("class name: " + className);
printf("func name: " + funcName);
Class c = Class.forName(className);
Method routine = c.getMethod(funcName, List.class);
printf("method: " +
routine.getGenericParameterTypes()[0].toString());
ret = (String) routine.invoke(args);
return ret;
}
}
And this is the class whose method is going to be called:
public class Math {
public static String C(List<String> args) {
int n, r;
n = Integer.parseInt(args.get(0));
r = Integer.parseInt(args.get(1));
Combination c = new Combination(n, r);
return String.valueOf(c.getTotal());
}
}
But I got the following exception:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:23)
Why?
Stefan Schulz - 20 Jan 2006 09:41 GMT
> ret = (String) routine.invoke(args);
> return ret;
Look at the invoke method once more. Hint: Which object is the method
invoked on? None (since it is static)? Well, maybe you should tell the
JVM that.
Chris Smith - 20 Jan 2006 15:17 GMT
moop? <samhng@gmail.com> wrote:
> ret = (String) routine.invoke(args);
> But I got the following exception:
> java.lang.IllegalArgumentException: wrong number of arguments
You should read the API documentation before using a method. If you
did, it would have told you that the first parameter to invoke is a
reference to the object on which to invoke the method; and that it is
ignored and should be null for a static method. You are trying to call
the method with no parameters.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
moop - 22 Jan 2006 04:30 GMT
Thx, it might be ignored.
moop - 23 Jan 2006 03:06 GMT
But here comes the continued exceptions:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:24)
Chris Smith - 24 Jan 2006 14:57 GMT
moop? <samhng@gmail.com> wrote:
> But here comes the continued exceptions:
>
[quoted text clipped - 7 lines]
> at
> com.gsta.enrus.services.el.FunctionHandler.execRoutine(FunctionHandler.java:24)
That's a wrapper for a different exception. Look further down the stack
trace.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation