筱进GG

123

java.lang.NoSuchMethodException

错误信息:

java.lang.NoSuchMethodException: cn.xxxxx.core.entity.DepartmentDO.setName

getMethod,getDeclaredMethod。看了下说明大概的意思就是getMethod只能调用public声明的方法,而getDeclaredMethod基本可以调用任何类型声明的方法


代码实现:

private List<DepartmentDO> convertDepartmentList(List<List<String>> list) {
List<String> titleList = list.get(0);
List<DepartmentDO> departmentDOList = new ArrayList<>();
for (int i = 1; i < list.size(); i++) {
List<String> proList = list.get(i);
DepartmentDO department = new DepartmentDO();
Class<?> clazz = department.getClass();
for (int i1 = 0; i1 < proList.size(); i1++) {
String str = DepartmentEnum.getPro(titleList.get(i1));
String methodName = "set" + str.substring(0, 1).toUpperCase() + str.substring(1, str.length());


try {
Method method = clazz.getDeclaredMethod(methodName, String.class);
method.invoke(department, proList.get(i1));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
departmentDOList.add(department);


}
return departmentDOList;
}

解决:

method.invoke

需要invoke方法为反射方法添加参数!

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaokejin521/article/details/79959936
文章标签: java
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

java.lang.NoSuchMethodException

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭