Blankj/AndroidUtilCode · error · NullPointerException
{}
Error message
{} What it means
ObjectUtils.requireNonNull() and requireNonNulls() throw NullPointerException (without a message in the no-arg overload, or with a custom tip in the two-arg overload) when the passed reference is null. This mirrors java.util.Objects.requireNonNull but is part of the utilcode library. The requireNonNulls() varargs variant throws if the varargs array itself is null or if any element is null.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ObjectUtils.java:214
*/
public static <T> int compare(T a, T b, @NonNull Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
/**
* Checks that the specified object reference is not {@code null}.
*/
public static <T> T requireNonNull(T obj) {
if (obj == null) throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is.
*/
public static <T> T requireNonNull(T obj, String ifNullTip) {
if (obj == null) throw new NullPointerException(ifNullTip);
return obj;
}
/**
* Require the objects are not null.
*
* @param objects The object.
* @throws NullPointerException if any object is null in objects
*/
public static void requireNonNulls(final Object... objects) {
if (objects == null) throw new NullPointerException();
for (Object object : objects) {
if (object == null) throw new NullPointerException();
}
}
/**
* Return the nonnull object or default object.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Ensure the value is non-null before calling — use ObjectUtils.getOrDefault(obj, default) if a default is acceptable.
- Use the two-arg overload requireNonNull(obj, "descriptive tip") for clearer error messages.
- For varargs, ensure no element is null: pre-filter or validate with ObjectUtils.isEmpty() checks.
- If the value may legitimately be null, use Optional or a null-safe alternative instead of requireNonNull.
Example fix
// before String name = ObjectUtils.requireNonNull(fetchName()); // throws NPE if null // after String name = ObjectUtils.requireNonNull(fetchName(), "name must not be null"); // or with fallback: String name = ObjectUtils.getOrDefault(fetchName(), "default");
Defensive patterns
Strategy: validation
Validate before calling
// Validate before calling requireNonNull
T value = potentiallyNullSource();
if (value != null) {
ObjectUtils.requireNonNull(value, "descriptive tip");
} else {
// handle null case
value = defaultValue;
} Type guard
static <T> boolean isNonNull(T obj) {
return obj != null;
}
// Use: if (isNonNull(value)) { ... } else { ... } Try / catch
try {
ObjectUtils.requireNonNulls(obj1, obj2, obj3);
} catch (NullPointerException e) {
// Handle: one or more arguments were null
Log.e(TAG, "Required object was null", e);
throw new IllegalStateException("Missing required dependency", e);
} Prevention
- Prefer the two-arg overload requireNonNull(obj, "tip") for actionable error messages.
- Use ObjectUtils.getOrDefault(obj, default) when a fallback is acceptable.
- Pre-check with obj != null before calling requireNonNull if null is a valid business case.
- For varargs requireNonNulls(), validate each element in a loop before the call.
When it happens
Trigger: Calling ObjectUtils.requireNonNull(null); ObjectUtils.requireNonNull(null, "required field"); or ObjectUtils.requireNonNulls(obj1, null, obj3) where any argument is null.
Common situations: Passing an uninitialized field or a value returned from a nullable API; method parameter validation where caller omitted a required value; deserialized or fetched data that resolved to null; varargs misuse where the array itself is null.
Related errors
- comparator must not be null
- segment of <{segment}> is illegal
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/c6c698653a233266.
Report an issue: GitHub.