简体   繁体   English

java.lang.IllegalArgumentException:字符串数组上的参数类型不匹配

[英]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.

相关问题 java.lang.IllegalArgumentException:参数类型不匹配 - java.lang.IllegalArgumentException: argument type mismatch Java RMI提供java.lang.IllegalArgumentException:参数类型不匹配 - java RMI is giving java.lang.IllegalArgumentException: argument type mismatch java.lang.IllegalArgumentException:使用Reflection时参数类型不匹配 - java.lang.IllegalArgumentException: argument type mismatch while using Reflection faultString:java.lang.IllegalArgumentException:参数类型不匹配 - faultString: java.lang.IllegalArgumentException: argument type mismatch 无法调用方法:java.lang.IllegalArgumentException:参数类型不匹配 - Could not invoke method: java.lang.IllegalArgumentException: argument type mismatch Constructor.newInstance():java.lang.IllegalArgumentException:参数类型不匹配 - Constructor.newInstance(): java.lang.IllegalArgumentException: argument type mismatch java.lang.IllegalArgumentException:Spring Controller中的参数类型不匹配 - java.lang.IllegalArgumentException: argument type mismatch in Spring Controller JavaFX java.lang.IllegalArgumentException:参数类型不匹配[场景构建器] - JavaFX java.lang.IllegalArgumentException: argument type mismatch [scene builder] java.lang.IllegalArgumentException:参数类型不匹配:当将表单值存储在数据库中时 - java.lang.IllegalArgumentException: argument type mismatch:when store the form values in Database JSF convertNumber抛出java.lang.IllegalArgumentException:参数类型不匹配错误 - JSF convertNumber throws java.lang.IllegalArgumentException: argument type mismatch Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM