使用带双[]参数的java反射时获取noSuchMethodException
我有以下方法签名:使用带双[]参数的java反射时获取noSuchMethodException,java,Java,我有以下方法签名: public synchronized List<Bet> acceptTheBets(Double[] odds, BrokerState bs) 公共同步列表接受赌注(双倍[]赔率,经纪人状态bs) 我正在尝试使用反射来调用此方法,但是我得到以下异常: java.lang.NoSuchMethodException: regions.BettingCenter.AcceptheBetts([D,entities.BrokerState) 为什么会导致NoS
public synchronized List<Bet> acceptTheBets(Double[] odds, BrokerState bs)
公共同步列表接受赌注(双倍[]赔率,经纪人状态bs)
我正在尝试使用反射来调用此方法,但是我得到以下异常:
java.lang.NoSuchMethodException:
regions.BettingCenter.AcceptheBetts([D,entities.BrokerState)
为什么会导致
NoSuchMethodException
?下面是一个关于如何使用此方法的快速示例
public class A {
public static void main(String[] args) {
try {
Object a = new double[]{1.0, 2.0};
System.out.println(A.class.getMethod("getStrings", double[].class).invoke(new A(), a));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized List<String> getStrings(double[] array){
return DoubleStream.of(array).boxed().map(d -> "" + d).collect(Collectors.toList());
}
}
将通过以下方式调用:
//double[]
A.class.getMethod("getStrings", double[].class)
.invoke(new A(), (Object)new double[]{1.0, 2.0}));
//Double[]
A.class.getMethod("getStrings", Double[].class)
.invoke(new A(), (Object)new Double[]{1.0, 2.0}));
请注意所传递的类型和参数的差异。下面是一个关于如何使用它的快速示例
public class A {
public static void main(String[] args) {
try {
Object a = new double[]{1.0, 2.0};
System.out.println(A.class.getMethod("getStrings", double[].class).invoke(new A(), a));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized List<String> getStrings(double[] array){
return DoubleStream.of(array).boxed().map(d -> "" + d).collect(Collectors.toList());
}
}
将通过以下方式调用:
//double[]
A.class.getMethod("getStrings", double[].class)
.invoke(new A(), (Object)new double[]{1.0, 2.0}));
//Double[]
A.class.getMethod("getStrings", Double[].class)
.invoke(new A(), (Object)new Double[]{1.0, 2.0}));
请注意类型和传递的参数之间的差异。
[D
是double[]
不是double[]
。检查。看起来您传递的参数类型错误。[D
是double[]
不是double[]
。检查。看起来您传递的参数类型错误。请发布代码。请添加您正在使用的反射代码。这不是请发布代码。请添加您正在使用的反射代码。这不是