为什么 int num = Integer.getInteger(“123”) 会抛出 NullPointerException?
以下代码抛出 NullPointerException
:
int num = Integer.getInteger("123");
我的编译器是否在 null 上调用 getInteger
,因为它是静态的?这没有任何意义!
发生什么事了?
The following code throws NullPointerException
:
int num = Integer.getInteger("123");
Is my compiler invoking getInteger
on null since it's static? That doesn't make any sense!
What's happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
总体情况
这里有两个问题:
Integer getInteger(String)
没有按照您的想法进行操作null
Integer
到int
的赋值会导致自动拆箱Integer
为null
,因此抛出NullPointerException
解析
(String) "123"
为(int) 123
,您可以使用例如int Integer.parseInt(String)
。参考文献
Integer
API 参考static int parseInt(String)
static Integer getInteger(String)
On
Integer.getInteger
这是文档的内容不得不说一下这个方法的作用:
换句话说,此方法与将
String
解析为int/Integer
值无关,而是与System.getProperty
方法。诚然,这可能是相当令人惊讶的。不幸的是,该库有这样的惊喜,但它确实给您上了宝贵的一课:始终查找文档以确认方法的作用。
巧合的是,这个问题的一个变体出现在 谜题者归来:Schlock 和 Awe (TS-5186),Josh Bloch 和 Neal Gafter 的 2009 年 JavaOne 技术会议演示。这是总结幻灯片:
完整起见,还有这些类似于
Integer.getInteger:
Boolean.getBoolean(String)
Long.getLong(String)
相关问题
关于自动拆箱
当然,另一个问题是
NullPointerException
被抛出。为了关注这个问题,我们可以将代码片段简化如下:这里引用了《Effective Java》第 2 版第 49 条:“优先选择原始类型而不是装箱原始类型”:
在某些地方,您别无选择,只能使用装箱基元,例如泛型,但除此之外,您应该认真考虑使用装箱基元的决定是否合理。
相关问题
The Big Picture
There are two issues at play here:
Integer getInteger(String)
doesn't do what you think it doesnull
in this caseInteger
toint
causes auto-unboxingInteger
isnull
,NullPointerException
is thrownTo parse
(String) "123"
to(int) 123
, you can use e.g.int Integer.parseInt(String)
.References
Integer
API referencesstatic int parseInt(String)
static Integer getInteger(String)
On
Integer.getInteger
Here's what the documentation have to say about what this method does:
In other words, this method has nothing to do with parsing a
String
to anint/Integer
value, but rather, it has to do withSystem.getProperty
method.Admittedly this can be quite a surprise. It's unfortunate that the library has surprises like this, but it does teach you a valuable lesson: always look up the documentation to confirm what a method does.
Coincindentally, a variation of this problem was featured in Return of the Puzzlers: Schlock and Awe (TS-5186), Josh Bloch and Neal Gafter's 2009 JavaOne Technical Session presentation. Here's the concluding slide:
For completeness, there are also these methods that are analogous to
Integer.getInteger
:Boolean.getBoolean(String)
Long.getLong(String)
Related questions
On autounboxing
The other issue, of course, is how the
NullPointerException
gets thrown. To focus on this issue, we can simplify the snippet as follows:Here's a quote from Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives:
There are places where you have no choice but to use boxed primitives, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.
Related questions
来自 http://konigsberg.blogspot.com/2008/ 04/integergetinteger-are-you-kidding-me.html:
你想要这个:
From http://konigsberg.blogspot.com/2008/04/integergetinteger-are-you-kidding-me.html:
You want this:
请检查该方法的文档 getInteger()。
在此方法中,
String
参数是一个系统属性,用于确定具有指定名称的系统属性的整数值。正如此处所述,“123”不是任何系统属性的名称。如果您想将此字符串转换为
int
,请使用该方法:int num = Integer.parseInt("123")
。Please check documentation of the method getInteger().
In this method, the
String
parameter is a system property that determines the integer value of the system property with the specified name. "123" is not the name of any system property, as discussed here.If you want to convert this String to
int
, then use the method asint num = Integer.parseInt("123")
.