为什么int num = Integer.getInteger(" 123")抛出NullPointerException? [英] Why does int num = Integer.getInteger("123") throw NullPointerException?

查看:157
本文介绍了为什么int num = Integer.getInteger(" 123")抛出NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码抛出 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:


  • 整数getInteger(字符串)没有做你认为它做的事情


    • 它返回 null 在这种情况下

    • Integer getInteger(String) doesn't do what you think it does
      • It returns null in this case

      • 由于整数 null 抛出NullPointerException

      • Since the Integer is null, NullPointerException is thrown

      解析(字符串)123(int)123 ,你可以使用例如 int Integer.parseInt(String)

      To parse (String) "123" to (int) 123, you can use e.g. int Integer.parseInt(String).

      • Java Language Guide/Autoboxing
      • static int parseInt(String)
      • static Integer 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 or null, or if the property does not have the correct numeric format, then null is returned.

      换句话说,此方法与解析 String 无关到 int / Integer 值,而是与 System.getProperty 方法。

      In other words, this method has nothing to do with parsing a String to an int/Integer value, but rather, it has to do with System.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.

      同样,一个变种这个问题出现在 Puzzlers的回归:Schlock和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:


      道德




      • 潜伏在图书馆中的奇怪和可怕的方法


        • 有些人听起来无害无名


      • 确保你正在调用正确的方法

      • 阅读库文档


      • 不违反原则最简单的惊讶

      • 不要违反抽象层次结构

      • 不要对不同的行为使用相似的名称

      为了完整性,还有一些类似<$ c的方法$ c> Integer.getInteger :

      For completeness, there are also these methods that are analogous to Integer.getInteger:

      • Boolean.getBoolean(String)
      • Long.getLong(String)
      • Most Astonishing Violation of the Principle of Least Astonishment
      • Most awkward/misleading method in 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的引用,第49项:首选原始类型为盒装基元:

      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 throw NullPointerException. 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.

      • 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(&quot; 123&quot;)抛出NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆