[英]java.lang.IllegalArgumentException: argument type mismatch on string array
This is my code to invoke a method dynamically: 这是我动态调用方法的代码:
String[] parameters = new String[requiredParameters.length];
//here i put some values in the parameters array
method = TestRecommendations.class.getMethod("level1ClassSimilarityForUser",
String[].class);
System.out.println(":" + parameters[0] + ":");
results = (ResultSet) method.invoke(new TestRecommendations(), parameters)
parameters
is a string array, and this is the declaration of my level1ClassSimilarityForUser
method parameters
是一个字符串数组,这是我的level1ClassSimilarityForUser
方法的声明
public ResultSet level1ClassSimilarityForUser(String[] userURI) {
I am getting this error: 我收到此错误:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
invoke
expects an Object[]
as second argument (varargs is just a convenience syntax). invoke
期望一个Object[]
作为第二个参数(varargs只是一种方便的语法)。 I think in your case the String[]
is not taken as the first vararg argument, but the complete vararg Object[]
and thus your single strings are used as arguments which does not match String[]
. 我认为在你的情况下,
String[]
不作为第一个vararg参数,但完整的vararg Object[]
因此你的单个字符串被用作与String[]
不匹配的参数。
In your case, explicitly wrapping your parameters in an Object
array before giving it to invoke
should work. 在您的情况下,在给予
invoke
之前将参数显式地包装在Object
数组中应该有效。
So do results = (ResultSet) method.invoke(new TestRecommendations(), new Ojbect[] { parameters })
instead 所以
results = (ResultSet) method.invoke(new TestRecommendations(), new Ojbect[] { parameters })
代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.