我有以下代码
private static String getAddressLine(Addresses addresses) {
if (addresses.getAddrCivicNo() == null || addresses.getStName() == null)
{
return null;
}
StringBuilder addressLine = new StringBuilder("");
addressLine.append(Optional.ofNullable(addresses.getAddrCivicNo()).get());
addressLine.append(" ");
addressLine.append(Optional.ofNullable(addresses.getAddrPreStrTypeDesignator()).get());
addressLine.append(" ");
addressLine.append(Optional.ofNullable(addresses.getStName()).get());
addressLine.append(", ");
addressLine.append(Optional.ofNullable(addresses.getAddrPostStrTypeDesignator()).get());
addressLine.append(" ");
addressLine.append(Optional.ofNullable(addresses.getAddrPostStrDirectionDesignator()).get());
addressLine.append(" ");
addressLine.append(Optional.ofNullable(addresses.getAddrPreStrDirectionDesignator()).get());
addressLine.append(" ");
addressLine.append(Optional.of(addresses.getAddrUnitDesignatorTypeCd()).get());
addressLine.append(" ");
addressLine.append(Optional.of(addresses.getAddrUnitNo()).get());
return addressLine.toString();
}
如果我的任何字段是null,我会NoSuchElementException因调用而收到错误get()。如果我删除get()我要附加的调用Optional.empty,但如果字段是 ,我不想附加任何内容null。有什么建议么?
尚方宝剑之说
潇湘沐
皈依舞
慕村225694
相关分类