There is only one parameter in the Java method is an array, we can not take it for granted in the reflection of the song array, when the array in the time to represent a number of parameters.
Two arrays is not a meaning ah.
We should convert the array to objet so that it represents a parameter.
Import Java.lang.reflect.method;public class Methodtest {public void A (string[] args) {System.out.println ("a");} public static void Main (string[] args) throws Exception {methodtest obj = new Methodtest (); Method m = Obj.getclass (). GetMethod ("A", String[].class); M.invoke (obj, new string[1]); New STRING[1] is actually null, is a objectm.invoke (obj, (Object) new string[] {}); This force is converted to object, so it is also Objectm.invoke (obj, new string[] {}); Error, here is the number of arrays, is an object array, a method has only one parameter, so error}}
The result is
A
A
Exception in thread "main"
Java.lang.IllegalArgumentException:wrong Number of arguments
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:57)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:606)
Original link: http://huangyunbin.iteye.com/blog/2179144
Wrong number of Arguments,java method reflection when array parameter pits