Historically, we have rarely intentionally thrown a NullPointerException
in the Spring Framework.
Instead, we aim to throw either an IllegalArgumentException
or IllegalStateException
instead of a NullPointerException
.
However, changes to the code in recent times have introduced the use of Objects.requireNonNull(Object)
which throws a NullPointerException
without an explicit error message.
The latter ends up providing less context than a NullPointerException
thrown by the JVM (since Java 14) due to actually de-referencing a null-pointer. See https://openjdk.org/jeps/358.
In light of that, we have decided to revise our current use of Objects.requireNonNull(Object)
by replacing it with Assert.notNull()
, Assert.state()
, Objects.requireNonNull(T, String)
, or Objects.requireNonNull(T, Supplier)
as appropriate.
However, we will only use one of the Objects.requireNonNull(...)
variants when we are required to throw a NullPointerException
in order to comply with a third-party contract such as Reactive Streams.