java - Exception when invoke reflection Class.getMethod -
i have below code invokes method using reflection. getting,
java.lang.illegalargumentexception: wrong number of arguments
exception @ method.invoke. reason?
public class b { public static void main(string[] args) { a = new a(); method m; try { m = a.getclass().getmethod("m3",integer.class); m.invoke(a); } catch (exception e) { e.printstacktrace(); } } } public class { public void m3(integer x){ system.out.println("ssss"); } }
the invoke(object, object...)-method takes it's first parameter object method should invoked on (as did correctly) , then variable-length parameter parameters should passed to method call.
in case, you're forgetting methods integer-parameter. method you're trying call a.m3(), doesn't exist in class.
the correct call be:
m.invoke(a, 12); // or int/integer it's second parameter
Comments
Post a Comment