Java 枚举行为
我们有以下枚举:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你没有说“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 islong
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, withc.setComponentType()
? The way you're doing it pretty much loses you all the value of enums.您说在测试中调用了 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?
你在哪里初始化c?我怀疑这就是问题所在,并且您收到的 NullPointerException 实际上来自 setComponentTypeId 调用而不是 getter。
使用您的枚举代码(无需修改)我运行了以下命令:
我的输出:
然而;如果 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:
My Output:
However; If c is not initialized properly, you get a NullPointerException when you attempt to call c.setComponentTypeId for the first time.