I am trying to run the following code:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Reflection { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, IllegalArgumentException { Class<Cls> cls = Cls.class; Method[] methods = cls.getMethods(); for (Method m : methods) { m.invoke(cls); } } } class Cls { public static void method1() { System.out.println("Method1"); } public static void method2() { System.out.println("Method2"); } }
I keep getting an IllegalArgumentException: the wrong number of arguments, even if the two methods take no arguments.
I tried passing null
to the invoke
method, but that causes NPE.
source share