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.ma...@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.
> >

Reply via email to