-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NPE in max() and min() #1482
Comments
I'm not sure about allowing null values. An alternative is to prohibit In native Java the For now I suggest to filter the values in order to get sure List.of(null, 0, null, 1, null) . filter(Predicates::isNotNull) . min(); I will update the min/max documentation accordingly. |
I would ban |
Arithmetic operations should only process on defined element. E.g. So why A healthy codebase does not implement additional logical branches for every special case and his dog :-) We should do it as straight forward as possible. Therefore we will not include special null handling here. |
YEAH - and the user has to SUFFER if he uses null values, MUHAHAHAHA 👹 |
Even if max(1, null) shouldn’t be 1 it would be reasonable to assume that Eirik
|
@eirikm yes, that's right. We should throw something like an |
We can't easily intercept a min/max computation and map a NPE to an ArithmeticException. This is because min/max internally call minBy/maxBy, which take a custom Comparator. A custom Comparator can throw a NPE for another reason, so we can't catch the NPE and rethrow an ArtithmeticException. Therefore we don't do it more complicated than it should be. Plain Java acts like this: // throws NullPointerException
Stream.of(null, 1).min(Integer::compare);
// throws NullPointerException
Stream.of(1, null).min(Integer::compare); We will act the same way. This is just one extra check in min()/max(), if the head element is null. |
The simple solution works for all collections but TreeSet/RedBlackTree. Needs further investigation. Btw - all SortedMap and SortedSet implementations should at least override min() because that is the head() element (if present). |
Using min or max on a list containing a null value will either produce a NPE or Option.none:
The text was updated successfully, but these errors were encountered: