Wrong number of Arguments,java method reflection when array parameter pits

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.