小编bus*_*hra的帖子

为什么我得到Java.lang.IllegalArgumentException:使用Reflection调用varargs方法时参数的数量错误

我在类中有一个方法如下

  class Sample{

    public void doSomething(String ... values) {

      //do something
    }

    public void doSomething(Integer value) {

    }

  }


//other methods
.
.
.
Run Code Online (Sandbox Code Playgroud)

现在我得到IllegalArgumentException:下面的参数数量错误

Sample object = new Sample();
Method m = object.getClass().getMethod( "doSomething", String[].class );
String[] arr = {"v1","v2"};
m.invoke( object, arr ) // exception here
Run Code Online (Sandbox Code Playgroud)

java reflection

4
推荐指数
2
解决办法
2031
查看次数

标签 统计

java ×1

reflection ×1