问题:
I use object != null a lot to avoid NullPointerException . 我经常使用object != null来避免NullPointerException 。
Is there a good alternative to this? 有没有好的替代方法?
For example: 例如:
if (someobject != null) {
someobject.doCalc();
}
This avoids a NullPointerException , when it is unknown if the object is null or not. 如果不知道对象是否为null ,则可以避免NullPointerException 。
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以获取最新的方法。
解决方案:
参考一: https://stackoom.com/question/18dS/避免-空语句参考二: https://oldbug.net/q/18dS/Avoiding-null-statements
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/3209896
