java - java.lang.NoSuchMethodException:java.util.HashMap$EntrySet。()
问题描述
Caused by: java.lang.NoSuchMethodException: java.util.HashMap$EntrySet.<init>()
at java.lang.Class.getConstructor0(Class.java:2902) ~[na:1.7.0_80]
at java.lang.Class.getDeclaredConstructor(Class.java:2066) ~[na:1.7.0_80]
我的 JDK 版本是 1.7.0_80。当我执行以下 JUnit 测试代码时,错误消息将再次出现:
@Test
public void testGetDeclaredConstructor() throws NoSuchMethodException {
Map<String, Object> m1 = new HashMap<>();
Set<Map.Entry<String, Object>> entrySet = m1.entrySet();
Class clz = entrySet.getClass();
Constructor con = clz.getDeclaredConstructor();
con.setAccessible(true);
System.out.println("--------Test OK!");
}
解决方案
您可以通过列出声明的构造函数来得到答案。
Set<Map.Entry<Object, Object>> entrySet = new HashMap<>().entrySet();
Constructor<?>[] declaredConstructors = entrySet.getClass().getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor);
}
java.util.HashMap$EntrySet(java.util.HashMap)
因此,有一个构造函数,它以 HashMap 作为参数。因此,getDeclaredConstructor()
, (getDeclaredConstructor(Class<?>... parameterTypes)
没有参数) 试图获取一个不存在的构造函数。
推荐阅读
- r - 我的 group by 似乎没有在磁盘框架中工作
- marklogic - MarkLogic TDE 框架是否支持包含可重用的模板片段?
- excel - 由于按钮,Excel VBA 文件打开速度很慢
- javascript - 在反应中单击img时添加一个圆圈
- winapi - Direct2d 无论窗口大小如何,都可以通过监视器大小在窗口上绘制吗?
- javascript - 如何在 HTML JavaScript 中显示输入文本中的图像?
- r - 在 tufte_handout 代码块中包含换行符?
- c++ - 我的代码出现错误。问题:从前序和中序遍历构造二叉树
- database - 更新经典 ASP 网站 MS 访问数据库
- ios - 将字典拆分为键数组和值数组