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