[Item 55] Consider possibility of NPE
onacit opened this issue · 0 comments
onacit commented
In following two lines,
https://github.com/jbloch/effective-java-3e-source-code/blob/master/src/effectivejava/chapter8/item55/Max.java#L28-L29
- The
e.compareTo(result) > 0
part is evaluated inif
block. - And then the
requireNonNull(e)
part follows in that block.
Do we have any sense for the requireNonNull
call?
An NPE may be thrown in the e.compareTo
condition, I think.
Shouldn't it be,
// if (result == null || (e != null && e.compareTo(result) > 0))
// result = e;
Or, when each e
in the collection is lazily checked for the null
,
// if (result == null || Objects.requireNonNull(e).compareTo(result) > 0)
// result = e;