简体   繁体   English

NullPointer异常处理

[英]NullPointer Exception handling

I have been going through some site where they talk about how to prevent NPE. 我一直在浏览一些网站,他们谈论如何预防NPE。 There I saw people say use null != object but I don't understand why. 在那里我看到有人说使用null != object但我不明白为什么。 What is the difference between doing the below in java? 在Java中执行以下操作有什么区别?

if(null != object) 

vs VS

if(object != null)

There is no difference between the two in the effect. 两者在效果上没有区别。

However there is a school of thinking where it is recommended to use un-assignable (constant) values on the left hand side of a operator. 但是,有一种思路是建议在操作员的左侧使用不可分配的(常量)值。 Because this reduces the risk of unintended assignment (this is from the time where C compilers have been not warning about it). 因为这样可以减少意外分配的风险(这是从C编译器未对其进行警告的时间开始的)。

// this is dangerous when misstyped
if (object = null) {

The argument that writing the null first really reduces the effect of misstyping is however pretty weak. 然而,首先写空值确实可以减少错误输入的影响的论点非常微弱。 Especially when not using "==" but "!=" or "<". 特别是当不使用“ ==”而是“!=”或“ <”时。 So I would say, ignore those recommendations. 所以我想说,忽略那些建议。

There are however some situations where order is helpfull to prevent NPE: 但是,在某些情况下,订购有助于预防NPE:

if ("string".equals(object))

In this case you dont get an NPE when "object" is null. 在这种情况下,当“ object”为null时,您不会获得NPE。

What is the difference between doing the below in java? 在Java中执行以下操作有什么区别?

  • There is no difference in terms of what the code means. 在代码含义上没有区别。

  • There is no difference in terms of code safety / robustness. 在代码安全性/鲁棒性方面没有区别。

In some languages, the if (null == object) pattern helps to avoid this mistake: 在某些语言中, if (null == object)模式有助于避免此错误:

 if (object = null)

... which accidentally trashes the value of the object variable. ...这会意外破坏object变量的值。

But in Java, an if statement requires a condition expression whose type is boolean , and the type of object == null is NOT boolean. 但是在Java中, if语句需要条件表达式,其类型为boolean ,而object == null的类型不是布尔值。 Hence that particular mistake / typo is guaranteed to give a compilation error. 因此,可以确保特定的错误/错字会导致编译错误。


Digression ... 离题...

The case where you plausibly could get into trouble with '==' versus '=' in classic Java is this: 您可能在经典Java中遇到'=='而不是'='的情况是:

 if (flag == true)

or 要么

 if (flag == false)

However, no experienced Java programmer would write that. 但是,没有经验丰富的Java程序员会写那个。 Instead they would write: 相反,他们会写:

 if (flag)

and

 if (!flag)

With Java 5 and onwards, autoboxing of Boolean can get you into trouble. 在Java 5及更高版本中, Boolean自动装箱会给您带来麻烦。 However, in that case you are doing something that is inadvisable anyway. 但是,在这种情况下,您所做的事情无论如何都是不明智的。 Consider this: 考虑一下:

 Boolean TRUE = true;
 Boolean TOO_TRUE = new Boolean(true);  // generates a new instance ...

 if (TRUE == TOO_TRUE) {  // should be TRUE.equals(TRUE)
     ....
 }

Again, an experienced programmer would avoid using boxed types unnecessarily, and avoid testing them with == . 再次,有经验的程序员将避免不必要地使用装箱的类型,并避免使用==对其进行测试。 Besides, you would probably get a warning for this from an IDE or from a static anaylser like FindBugs or PMD. 此外,您可能会从IDE或从静态分析器(如FindBugs或PMD)获得此警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM