I use object != null
a lot to avoid NullPointerException
. 我常用object != null
来避免NullPointerException
。 html
Is there a good alternative to this? 有没有好的替代方法? java
For example: 例如: api
if (someobject != null) { someobject.doCalc(); }
This avoids a NullPointerException
, when it is unknown if the object is null
or not. 若是不知道对象是否为null
,则能够避免NullPointerException
。 oracle
Note that the accepted answer may be out of date, see https://stackoverflow.com/a/2386013/12943 for a more recent approach. 请注意,接受的答案可能已过时,请参阅https://stackoverflow.com/a/2386013/12943以获取最新的方法。 app