//会报空指针,list.get(0).getXXXXXX 返回了null,导致of() 方法抛出空指针
BigDecimal s = Optional.of(list.get(0).getXXXXXX).orElse(BigDecimal.ZERO);
//正常
BigDecimal s = Optional.ofNullable(list.get(0).getXXXXXX).orElse(BigDecimal.ZERO);
Optional.of() 源码
public static <T> Optional<T> of(T value) {
return new Optional<>(value);
}
private Optional(T value) {
this.value = Objects.requireNonNull(value);
}
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
https://www.python100.com/html/HUW6895UF7D8.html
标签:BigDecimal,get,getXXXXXX,value,Optional,得空,指针 From: https://www.cnblogs.com/czzz/p/17968570