Why do I get Java Lang.illegalargumentexception: wrong number of parameters when calling method with varargs using reflection
•
Java
See English answers > wrong number of arguments error when invoking a method
class Sample{ public void doSomething(String ... values) { //do something } public void doSomething(Integer value) { } } //other methods . . .
Now I get the illegalargumentexception: the number of parameters below is wrong
Sample object = new Sample(); Method m = object.getClass().getmethod( "doSomething",String[].class ); String[] arr = {"v1","v2"}; m.invoke( object,arr ) // exception here
Solution
Wrap the string array in the object array:
Sample object = new Sample(); Method m = object.getClass().getmethod("doSomething",String[].class); String[] arr = {"v1","v2"}; m.invoke(object,new Object[] {arr});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码