Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong number of arguments error when invoking a method

I have class AClass and a method someMethod that gets an Object array as parameter.

public class AClass {     public void someMethod(Object[] parameters) {     } } 

In main, when I try to invoke this method on the the object that I have created and give an object array as a parameter to this method

Object[] parameters; // lets say this object array is null Class class = Class.forName("AClass"); Object anObject = class.newInstance();  Method someMethod = class.getDeclaredMethod("someMethod", parameters.getClass()); someMethod.invoke(anObject, parameters); 

I get "wrong number of arguments error". What am i missing?

like image 231
iargin Avatar asked Nov 18 '11 22:11

iargin


People also ask

What does wrong number of arguments mean?

The number of arguments to a procedure must match the number of parameters in the procedure's definition. This error has the following causes and solutions: The number of arguments in the call to the procedure wasn't the same as the number of required arguments expected by the procedure.

How many arguments do we pass in methods?

This method has four parameters: the loan amount, the interest rate, the future value and the number of periods.

How many arguments can a method call have in Java?

There is a technical maximum of 255 parameters that a method can have.


2 Answers

That will be all right.

Object[] parameters = {new Object()}; // lets say this object array is null Class clas = Class.forName("AClass"); Object anObject = clas.newInstance();  Object[] param = {parameters};  Method someMethod = clas.getDeclaredMethod("someMethod", parameters.getClass()); someMethod.invoke(anObject, param); 

Be careful about the second parameter of the invoke method. It's Object[] itself, and the argument type of your method is Object[] too.

like image 167
biaobiaoqi Avatar answered Sep 20 '22 21:09

biaobiaoqi


To expand a bit on what orien and biaobiaoqi are saying . . .

What's probably confusing you here is that Method.invoke(Object, Object...) can usually just take the arguments "inline", so to speak; when the compiler sees something like someMethod.invoke(someObject, arg1, arg2), it implicitly creates an array new Object[]{arg1, arg2} and then passes that array to Method.invoke. Method.invoke then passes the elements of that array as arguments to the method you're invoking. So far, so good.

But when the compiler sees something like someMethod.invoke(someObject, someArray), it assumes that you've already packaged the arguments into an array; so it won't repackage them again. So then Method.invoke will try to pass the elements of someArray as arguments to the method you're invoking, rather than passing someArray itself as an argument.

(This is always how the ... notation works; it accepts either an array containing elements of the appropriate type, or zero or more arguments of the appropriate type.)

So, as orien and biaobiaoqi have said, you need to rewrap your parameters into an additional array, new Object[] {parameters}, so that parameters itself ends up being passed into your method.

Does that make sense?

like image 22
ruakh Avatar answered Sep 22 '22 21:09

ruakh