問題描述
以下代碼拋出NullPointerException
:
int num = Integer.getInteger("123");
我的編譯器是否在 null 上調用 getInteger
,因為它是靜態的?這沒有任何意義!
Is my compiler invoking getInteger
on null since it's static? That doesn't make any sense!
發生了什么事?
推薦答案
大局
這里有兩個問題:
The Big Picture
There are two issues at play here:
Integer getInteger(String)
沒有做你認為它做的事情- 在這種情況下返回
null
Integer getInteger(String)
doesn't do what you think it does- It returns
null
in this case
- 由于
Integer
為null
,NullPointerException
被拋出
要將
(String) "123"
解析為(int) 123
,您可以使用例如int Integer.parseInt(String)
.To parse
(String) "123"
to(int) 123
, you can use e.g.int Integer.parseInt(String)
.- Java 語言指南/自動裝箱
static int parseInt(String)
靜態整數 getInteger(String)
以下是文檔中關于此方法的作用的內容:
Here's what the documentation have to say about what this method does:
public static Integer getInteger(String nm)
:確定指定名稱的系統屬性的整數值.如果沒有指定名稱的屬性,如果指定的名稱為空或null
,或者如果該屬性沒有正確的數字格式,則返回null
.public static Integer getInteger(String nm)
: Determines the integer value of the system property with the specified name. If there is no property with the specified name, if the specified name is empty ornull
, or if the property does not have the correct numeric format, thennull
is returned.換句話說,該方法與將
String
解析為int/Integer
值無關,而是與System.getProperty
方法.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.
巧合的是,這個問題的一個變體出現在 謎題歸來:Schlock and Awe (TS-5186),Josh Bloch 和 Neal Gafter 的 2009 JavaOne 技術會議演示文稿.這是最后一張幻燈片:
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:
- 圖書館中潛伏著奇怪而可怕的方法
- 有些名字聽起來無傷大雅
- 確保調用正確的方法
- 閱讀庫文檔
- 不要違反最小驚訝原則
- 不要違反抽象層次結構
- 不要為截然不同的行為使用相似的名稱
為了完整起見,還有這些方法類似于
Integer.getInteger
:For completeness, there are also these methods that are analogous to
Integer.getInteger
:Boolean.getBoolean(String)
Long.getLong(String)
- 最令人震驚的對最小驚訝原則的違反一個>
- Java Base API 中最尷尬/誤導性的方法?
當然,另一個問題是
NullPointerException
是如何被拋出的.為了專注于這個問題,我們可以將代碼片段簡化如下:The other issue, of course, is how the
NullPointerException
gets thrown. To focus on this issue, we can simplify the snippet as follows:Integer someInteger = null; int num = someInteger; // throws NullPointerException!!!
這里引用 Effective Java 2nd Edition,Item 49:Prefer primitive types to boxed primitives:
Here's a quote from Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives:
總而言之,只要您有選擇,就優先使用原語而不是盒裝原語.原始類型更簡單、更快.如果您必須使用盒裝圖元,請小心!自動裝箱減少了使用裝箱原語的冗長,但不會降低危險.當您的程序使用
==
運算符比較兩個裝箱原語時,它會進行身份比較,這幾乎肯定不是您想要的.當您的程序進行涉及裝箱和未裝箱原語的混合類型計算時,它會進行拆箱,而當您的程序進行拆箱時,它可能會拋出NullPointerException
.最后,當您的程序將原始值裝箱時,可能會導致成本高昂且不必要的對象創建.In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the
==
operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throwNullPointerException
. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.有些地方你別無選擇,只能使用盒裝圖元,例如泛型,否則您應該認真考慮使用盒裝原語的決定是否合理.
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.
- 什么是Java/C# 中的 int 和 Integer 的區別?
- 為什么 Java 中的自動裝箱允許我有 3 個可能的布爾值?
- 是否保證 new Integer(i) ==我在 Java 中?(是的?。?!)
- 在 Java 中比較兩個整數時會自動執行拆箱發生?(不!??!)
- Java noob:僅限對象的泛型?(是的,很遺憾)
- What is the difference between an int and an Integer in Java/C#?
- Why does autoboxing in Java allow me to have 3 possible values for a boolean?
- Is it guaranteed that new Integer(i) == i in Java? (YES!!!)
- When comparing two Integers in Java does auto-unboxing occur? (NO!!!)
- Java noob: generics over objects only? (yes, unfortunately)
這篇關于為什么 int num = Integer.getInteger("123") 會拋出 NullPointerException?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- It returns
- 在這種情況下返回