reflection: wrong number of arguments

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

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?
 
S

Stefan Schulz

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.
 
C

Chris Smith

moop? said:
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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
?

=?iso-8859-1?B?bW9vcJk=?=

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)
 
C

Chris Smith

moop? said:
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)

That's a wrapper for a different exception. Look further down the stack
trace.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top