我已经阅读了这个主题:避免!= null语句,但我没有看到好处.
如果返回空数组,则需要检查"实数"数组的位置以及空数组的位置.
getName() != null您必须使用,而不是使用类似的代码getName().length > 0.作为一个例子,我正在研究项目,我需要用少量的归档器来压缩一些数据,并决定选择尺寸最小的压缩数据.如果我从归档器"compress"方法返回null,我需要检查返回的值是否为null.如果我决定返回一个空数组,那么我还需要检查它是否为空.
我对吗?
我不想将null值传递给视图层,所以如果我执行以下操作:
public List<Object> getListObjFoo(){
List<Object> listObj = datasource.getAll();
return listObj != null ? listObj : new ArrayList<Object>();
}
Run Code Online (Sandbox Code Playgroud)
它应该被认为是好的还是坏的做法?谷歌的"Guava"中使用"可选"比这更好吗?为什么?
我有以下代码:
public final boolean doesExistById(Long id) {
return dataAccessObject.findById(id) != null;
}
public final boolean doesExistByName(String name) {
return dataAccessObject.findByName(name) != null;
}
public final boolean doesExistByDisplayName(String displayName) {
return dataAccessObject.findByDisplayName(displayName) != null;
}
public final boolean doesExistByWebId(String webId) {
return dataAccessObject.findByWebId(webId) != null;
}
Run Code Online (Sandbox Code Playgroud)
我的Product班级有属性id, name, displayName, wedId.
dataAccessObject.findBy____()返回一个类型的对象Product,如果它可以在数据存储中找到,或者null它不能.
如果可能的话,我想减少这段代码,因为我有许多需要上述doesExist()模式的对象.客户端代码只会知道其中一个属性.
我想到的一个可能的解决方案是:
public final boolean doesExist(Long id, String name, String displayName, String webId) {..}
Run Code Online (Sandbox Code Playgroud)
然后null在使用if语句确定哪个字段具有值时,将其用于未知字段.但还有另一种更优雅的方式吗?
我是一名初级开发人员,我使用java做网站开发。我知道推荐 org.apache.common.lang.StringUtils 因为它的 null 安全性。但是什么是空安全或空安全确切?为什么下面的代码很难看?
if( sth != null ) { ... }
我有一个非常简单的方法:
public int getScore() {
return game.gameWindow.userBestScore;
}
Run Code Online (Sandbox Code Playgroud)
问题是它可能发生该game对象或gameWindow不存在.我不想得到Null Pointer Exception.怎么能以正确的方式捕获它?我可以这样做吗:
public int getScore() {
try{
return game.gameWindow.userBestScore;
} catch(NullPointerException e){
return -1;
}
}
Run Code Online (Sandbox Code Playgroud) 假设我们有一个名为Intersection的类,带有一个findIntersect(line1, line2)方法.它返回一个名为的对象point,其中包含2个字段x和y坐标.现在,如果输入是2条平行线,那么与用户无法获得结果的最佳通信方式是什么?虽然示例是特定于行的,但问题是通用的 - 假设一个方法返回值对象,如果条件不匹配则返回什么?一些选项是:
null(问题:在许多地方读取,如果可能,应避免返回null返回值)hasNext()在Iterator?Exception?请让我知道最好的方法.
我不打算在下面的情况下进行多次空检查,而是计划添加一些可读的代码.可以借助java 8流/地图.有人可以帮我这个
private String getRailsServiceClass(IRailsComponent railsComponent) {
String serviceClass = "";
if (railsComponent != null && railsComponent.getRailOffer() != null && railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct() != null && railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct().getFareBreakdownList() != null &&
railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct().getFareBreakdownList().get(0).getPassengerFareList() != null && railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct().getFareBreakdownList().get(0).getPassengerFareList().get(0).getPassengerSegmentFareList() != null &&
railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct().getFareBreakdownList().get(0).getPassengerFareList().get(0).getPassengerSegmentFareList().get(0).getCarrierServiceClassDisplayName() != null) {
return railsComponent.getRailOffer().getRailProducts().get(0).getRailProduct().getFareBreakdownList().get(0).getPassengerFareList().get(0).getPassengerSegmentFareList().get(0).getCarrierServiceClassDisplayName();
}
return serviceClass;
}
Run Code Online (Sandbox Code Playgroud) 可能之前问过,但找不到。
我写了很多声明的形式:
if (bar.getFoo() != null) {
this.foo = bar.getFoo();
}
Run Code Online (Sandbox Code Playgroud)
我想到了三元运算符,但在我看来,这并没有让它变得更好:
this.foo = bar.getFoo() != null ? bar.getFoo() : this.foo;
Run Code Online (Sandbox Code Playgroud)
我的主要问题是在这两种情况下我都必须编写 bar.getFoo() 两次。当然我可以写一个辅助方法来解决这个问题,但我想知道是否有更优雅的解决方案。
不同于:避免 != null 语句,因为该问题不涉及将检查为 null 的值分配给值。
假设我有10行代码.任何行都可能抛出NullPointerException.我不想关心这些例外.如果一行抛出异常,我希望执行者跳转到下一行并继续前进.我可以在Java上这样做吗?如果是的话,请给我一些示例代码.
更新:(添加样本来源以获得更清晰的问题)
first.setText(prize.first);
second.setText(prize.second);
third.setText(prize.third);
fourth.setText(prize.fourth);
fifth.setText(prize.fifth);
sixth.setText(prize.sixth);
seventh.setText(prize.seventh);
eighth.setText(prize.eighth);
Run Code Online (Sandbox Code Playgroud)
假设我上面有8行代码.我想要的是:如果第4行(或5,6,...)抛出异常,所有其他代码行都能正常工作.当然我可以使用try ... catch来逐行捕获异常.但这种方式使我的来源非常复杂.
我在输入中有一些数据,我将不得不用它来设置POJO的所有属性.POJO可能部分设置.我的问题是仅在相关输入数据不为空时才设置属性.我知道我可以通过两种方式做到这一点:
if (input != null) {
obj.setData(input);
}
Run Code Online (Sandbox Code Playgroud)
要么
obj.setData(input != null ? input : obj.getData());
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种不那么难看的解决方案,对于具有大量属性的对象来说更好.