You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 28, 2022. It is now read-only.
For setters and constructors, we can also "inline" this check, by doing this:
public Foo(Bar bar) {
this.bar = Objects.requireNonNull(bar);
}
Note that this is only allowed if it is a direct assignment, otherwise, never inline it.
The text was updated successfully, but these errors were encountered:
yamgent
changed the title
Java: Use Objects#requireNonNull() for argument validation
Java: use Objects#requireNonNull() for argument validation
May 19, 2017
Google's Guava library also has more helper methods for different kinds of argument validation, although that means we will have a new dependency, so projects should probably write their own version of the helper methods:
A typical check for null in argument would be as follow:
We can use Java's
Objects#requireNonNull(T)
helper method instead. It automatically throws aNullPointerException
for us.For setters and constructors, we can also "inline" this check, by doing this:
Note that this is only allowed if it is a direct assignment, otherwise, never inline it.
The text was updated successfully, but these errors were encountered: