【问题标题】:getDeclaredMethod leading to java.lang.NoSuchMethodExceptiongetDeclaredMethod 导致 java.lang.NoSuchMethodException
【发布时间】:2016-03-22 07:42:23
【问题描述】:

在我的一个课程中,我在下面的代码行中调用了 BLH 类的 preOTPOOperations

Class<?> clazz = Class.forName("com.test.BLH");
Object obj = clazz.newInstance();
Class<?>[] paramTypes = new Class[4];
paramTypes[0]=String.class;
paramTypes[1]=String.class;
paramTypes[2]=Integer.class;
paramTypes[3]=COConfig.class;
Method m = clazz.getDeclaredMethod("preOTPOperations", paramTypes); 
String responseMessage = (String) m.invoke(obj, new Object[]{cardnumber, null, bankId, myConfig});

但是,当我尝试使用invoke() 调用上述BLH 的preOTPOperations 方法时,我得到了java.lang.NoSuchMethodException。

在 BLH 课程中,我有如下 preOTPOOperations。

public String preOTPOperations(String cardnumber, String mobileNumber, int bankid, COConfig coConfig){

    //some code goes here

}

不知道为什么我得到 NoSuchMethodException 尽管在具有公共访问说明符的 BLH 类中有 preOTPOperations。有人好心地提出解决方案。我错过了什么吗?提前致谢!

【问题讨论】:

    标签: java reflection nosuchmethoderror


    【解决方案1】:

    您需要将Integer.class 更改为int.classIntegerint 不是同一种类型,因为你指定了错误的类型,所以找不到方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2013-11-23
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多