美文网首页
object is not an instance of dec

object is not an instance of dec

作者: 东南枝下 | 来源:发表于2021-07-16 18:52 被阅读0次

在使用自省函数时遇到

java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]

代码如下

PropertyDescriptor proDesc = null;
try {
    proDesc = new PropertyDescriptor(propertyName, Header.class);
} catch (IntrospectionException e) {
    log.error("failed to find attribute field {}", e);
    return;
}
Method writeMethod = proDesc.getWriteMethod();
lines.forEach(line -> {
    // 将字段置空
    try {
        writeMethod.invoke(line, (Object) null);
    } catch (IllegalAccessException | InvocationTargetException e) {
        log.error("failed to empty attribute field value {}", e);
    }
});

网上大多数原因时说writeMethod.invoke没有传入一个实例,但在这里不是这个原因

原因:
line 的类型为Line.class 而不是Header.class
在定义PropertyDescriptor时类填错了

相关文章

网友评论

      本文标题:object is not an instance of dec

      本文链接:https://www.haomeiwen.com/subject/isibpltx.html