ReflectionUtil getField 抛出 NullPointerException

发布于 2024-10-20 01:24:49 字数 881 浏览 1 评论 0原文

我使用一个类,通过 FilterCriterionList (某种查找器)进行 Hibernate 查询,直到现在它一直运行良好并触发 NullPointerException,我完全不知道为什么会触发它。

这是使用以下值触发空指针的方法(在 ReflectionUtil 中)(请注意,向其抛出的其他值工作正常,只是这些值似乎给出了错误):

type = interface java.util.List

fieldName =参数

首先它抛出 NoSuchFieldException 并在第二次运行时(因为它在 field = getField(type.getSuperclass(), fieldName); 处再次调用)使其抛出 NullPointerException 并且停止死亡(所有这些都发生在我的 UnitTest 中,而不是实时环境)。

public static Field getField(Class type, String fieldName) {
        Field field = null;
        try {
            field = type.getDeclaredField(fieldName);
        } catch (Exception e) {
            if (!type.equals(Object.class)) {
                field = getField(type.getSuperclass(), fieldName);
            }
        }
        return field;
    }

关于为什么会发生这种情况的任何想法(或者我可以做些什么来解决它?)。我无法真正展示更多代码,因为它非常复杂并且是公司代码。

I use a class that makes my Hibernate Query through a FilterCriterionList (sort of finder) and it's always worked perfectly until now and triggers a NullPointerException and I have absolutely no idea as to why it's triggered.

This is the method (in ReflectionUtil) that triggers the nullpointer with the following values (mind you that the other values thrown at it work perfectly and it's just these that seem to give an error):

type = interface java.util.List

fieldName = parameter

First it throws the NoSuchFieldException and on it's second run (as it's called again at field = getField(type.getSuperclass(), fieldName);) makes it throw a NullPointerException and just stop dead (all of this happens in my UnitTest, not a live environment yet).

public static Field getField(Class type, String fieldName) {
        Field field = null;
        try {
            field = type.getDeclaredField(fieldName);
        } catch (Exception e) {
            if (!type.equals(Object.class)) {
                field = getField(type.getSuperclass(), fieldName);
            }
        }
        return field;
    }

Any ideas as to why this happens (or what I can do to fix it?). I can't really show off more code as it's quite complicated and it's company code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人接听 2024-10-27 01:24:49

java.util.List 是一个接口,因此对其调用 getSuperclass() 会导致 null

递归应用 getField() 的正确方法如下:

} catch (Exception e) {
    Class superclass = type.getSuperclass();
    if (superclass != null) {
        field = getField(superclass, fieldName);
    }
}

java.util.List is an interface, therefore calling getSuperclass() on it results in null.

The correct way to apply getField() recursively is the following:

} catch (Exception e) {
    Class superclass = type.getSuperclass();
    if (superclass != null) {
        field = getField(superclass, fieldName);
    }
}
一绘本一梦想 2024-10-27 01:24:49

您这样调用:

getField(List.class, "parameter");

它永远不会起作用,因为 List 是一个接口,并且它没有任何称为参数的字段。对于第二次调用,其为 null,因为接口 List 没有超类。

You called like this:

getField(List.class, "parameter");

It would have never worked as List is an interface and it doesen't have any field called parameter. For the second call, its' null because interface List doesnet have a superclass.

苦行僧 2024-10-27 01:24:49

来自 Class.getSuperclass 文档

返回代表的类
实体的超类(类,
接口、原始类型或 void)
由这个类代表。 如果这个
类代表对象
类、接口、原始类型、
或 void,则返回 null
。如果
该对象代表一个数组类
那么代表的 Class 对象
返回对象类。

From the Class.getSuperclass documentation

Returns the Class representing the
superclass of the entity (class,
interface, primitive type or void)
represented by this Class. If this
Class represents either the Object
class, an interface, a primitive type,
or void, then null is returned
. If
this object represents an array class
then the Class object representing the
Object class is returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文