java.lang.IllegalArgumentException: argument type mismatch on string array

前端 未结 1 1026
心在旅途
心在旅途 2021-01-19 15:56

This is my code to invoke a method dynamically:

String[] parameters = new String[requiredParameters.length];
//here i put some values in the parameters array         


        
1条回答
  •  礼貌的吻别
    2021-01-19 16:30

    invoke expects an Object[] as second argument (varargs is just a convenience syntax). 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[].
    In your case, explicitly wrapping your parameters in an Object array before giving it to invoke should work.
    So do results = (ResultSet) method.invoke(new TestRecommendations(), new Ojbect[] { parameters }) instead

    0 讨论(0)
提交回复
热议问题