Java 枚举行为

发布于 2024-08-13 07:08:13 字数 806 浏览 4 评论 0原文

我们有以下枚举:

public enum ComponentTypes {

    PDIFF(301),
    TDIFF(302),
    TADJ(303);

    private long componentTypeId;

    private ComponentTypes(long componentTypeId){
        this.componentTypeId = componentTypeId;
    }

    public Long getId(){
        return this.componentTypeId;
    }
}

在我们的一个测试设置中,我们执行 c.setComponentTypeId(ComponentTypes.TADJ.getId()) 但当 c.getComponentTypeId() 被调用时测试它抛出 NullPointerException,但 c.setComponentTypeId(303L) 按预期工作。使用枚举来设置值时我缺少什么?

编辑

看起来@Tom 直接指出了长/长的不一致。现在 getId() 返回 long 而不是 Long 它按预期工作。

编辑

看起来我之前说的是错误的自动装箱确实按预期工作在我刷新系统jvm等之后没有问题 - 这对我来说没有任何意义!

We have the following enumeration:

public enum ComponentTypes {

    PDIFF(301),
    TDIFF(302),
    TADJ(303);

    private long componentTypeId;

    private ComponentTypes(long componentTypeId){
        this.componentTypeId = componentTypeId;
    }

    public Long getId(){
        return this.componentTypeId;
    }
}

In one of our tests setup we do c.setComponentTypeId(ComponentTypes.TADJ.getId()) but when c.getComponentTypeId() is invoked in the test it throws NullPointerException, yet c.setComponentTypeId(303L) works as expected. What am I missing with use of the enum to set the value?

EDIT

Looks like @Tom was straight on with the long/Long inconsistency. Now that getId() returns long not Long it works as expected.

EDIT

Looks like what I said earlier was wrong autoboxing does work there as expected is no issue after I refreshed the system jvm etc -- which doesn't make any sense to me!

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

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

发布评论

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

评论(3

痴情 2024-08-20 07:08:13

你没有说“c”是什么类型,但我怀疑它的设置器没有按照你想象的那样做——NullPointerException表明自动拆箱出了问题。您枚举本身似乎没有问题,尽管当成员为 long 时返回 Long 是一种代码味道。

实际上,使用枚举的 ID 调用 c.setComponentTypeId() 是另一种代码味道。为什么不通过 c.setComponentType() 使用枚举本身?你这样做的方式几乎失去了枚举的所有价值。

You don't say what type "c" is, but I suspect its setter is not doing what you think it is -- the NullPointerException is an indication of auto-unboxing gone wrong. You enum itself doesn't appear to have an issue, although returning a Long when the member is long is a code smell.

Actually, calling c.setComponentTypeId() with the enum's ID is another code smell. Why aren't you using the enum itself, with c.setComponentType()? The way you're doing it pretty much loses you all the value of enums.

节枝 2024-08-20 07:08:13

您说在测试中调用了 setComponentId,但您没有包含此方法的源代码。您可以粘贴此内容,并展示测试中的“c”对象是如何创建的吗?

汤姆,如果他们使用支持自动装箱的最新 java 版本,那么 long/Long 肯定不重要吗?

You say in the test you call setComponentId, but you have not included the source for this method. Can you paste this, and also show how the 'c' object in the test is created?

Tom, if they are using a version of recent java supporting autoboxing surely long/Long won't matter?

木森分化 2024-08-20 07:08:13

你在哪里初始化c?我怀疑这就是问题所在,并且您收到的 NullPointerException 实际上来自 setComponentTypeId 调用而不是 getter。

使用您的枚举代码(无需修改)我运行了以下命令:

ComponentTypes c = ComponentTypes.PDIFF;
c.setComponentTypeId(ComponentTypes.TADJ.getId());
System.out.println(c.getComponentTypeId());

c.setComponentTypeId(303L);
System.out.println(c.getComponentTypeId());

我的输出:

303

303

然而;如果 c 未正确初始化,则当您第一次尝试调用 c.setComponentTypeId 时,您会收到 NullPointerException。

Where are you initializing c? I suspect this is the issue and that the NullPointerException you're receiving is actually coming from the setComponentTypeId call not the getter.

Using your code for the enum (with no modifications) I ran the following:

ComponentTypes c = ComponentTypes.PDIFF;
c.setComponentTypeId(ComponentTypes.TADJ.getId());
System.out.println(c.getComponentTypeId());

c.setComponentTypeId(303L);
System.out.println(c.getComponentTypeId());

My Output:

303

303

However; If c is not initialized properly, you get a NullPointerException when you attempt to call c.setComponentTypeId for the first time.

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