8281301: Map.of() calls with null argument should throw more contextual information.
Kasper Nielsen
kasperni at gmail.com
Wed Mar 1 10:59:54 UTC 2023
I've been in situations as well where a bit more context would have been very
useful for some collection NPEs.
While lambdas are overkill. I do think there are some more low-level solutions.
requireNonNull(k0) can be replaced with with requireNonNull(k0, "key is null");
For the indexed versions we could introduce:
public static <T> T requireIndexedNonNull(T obj, int index, String message) {
if (obj == null)
throw new NullPointerException(message + " is null at index " + index);
return obj;
}
and uses that wherever applicable.
/Kasper
On Wed, 1 Mar 2023 at 03:41, Stuart Marks <stuart.marks at oracle.com> wrote:
>
> > My proposed change is pretty small. Basically, a pattern I see with
> > some frequency is calling Objects.requireNonNull(object, "objectName")
> > before (or within) Map.of(), because otherwise there's not much of a
> > way to tell _which_ parameter caused the exception. Even just an index
> > and key/value hint would make tracking down these types of errors
> > easier without that bit of boilerplate.
> >
More information about the core-libs-dev
mailing list