Wrong number of arguments error when invoking a method

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.

Leave a Comment