{"record":{"id":"c6c698653a233266","repo":"Blankj/AndroidUtilCode","slug":"error-c6c698","errorCode":null,"errorMessage":"{}","messagePattern":"\\{\\}","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ObjectUtils.java","lineNumber":214,"sourceCode":"     */\n    public static <T> int compare(T a, T b, @NonNull Comparator<? super T> c) {\n        return (a == b) ? 0 : c.compare(a, b);\n    }\n\n    /**\n     * Checks that the specified object reference is not {@code null}.\n     */\n    public static <T> T requireNonNull(T obj) {\n        if (obj == null) throw new NullPointerException();\n        return obj;\n    }\n\n    /**\n     * Checks that the specified object reference is not {@code null} and\n     * throws a customized {@link NullPointerException} if it is.\n     */\n    public static <T> T requireNonNull(T obj, String ifNullTip) {\n        if (obj == null) throw new NullPointerException(ifNullTip);\n        return obj;\n    }\n\n    /**\n     * Require the objects are not null.\n     *\n     * @param objects The object.\n     * @throws NullPointerException if any object is null in objects\n     */\n    public static void requireNonNulls(final Object... objects) {\n        if (objects == null) throw new NullPointerException();\n        for (Object object : objects) {\n            if (object == null) throw new NullPointerException();\n        }\n    }\n\n    /**\n     * Return the nonnull object or default object.","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ObjectUtils.java#L196-L232","documentation":"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.","triggerScenarios":"Calling ObjectUtils.requireNonNull(null); ObjectUtils.requireNonNull(null, \"required field\"); or ObjectUtils.requireNonNulls(obj1, null, obj3) where any argument is null.","commonSituations":"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.","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."],"exampleFix":"// before\nString name = ObjectUtils.requireNonNull(fetchName()); // throws NPE if null\n\n// after\nString name = ObjectUtils.requireNonNull(fetchName(), \"name must not be null\");\n// or with fallback:\nString name = ObjectUtils.getOrDefault(fetchName(), \"default\");","handlingStrategy":"validation","validationCode":"// Validate before calling requireNonNull\nT value = potentiallyNullSource();\nif (value != null) {\n    ObjectUtils.requireNonNull(value, \"descriptive tip\");\n} else {\n    // handle null case\n    value = defaultValue;\n}","typeGuard":"static <T> boolean isNonNull(T obj) {\n    return obj != null;\n}\n// Use: if (isNonNull(value)) { ... } else { ... }","tryCatchPattern":"try {\n    ObjectUtils.requireNonNulls(obj1, obj2, obj3);\n} catch (NullPointerException e) {\n    // Handle: one or more arguments were null\n    Log.e(TAG, \"Required object was null\", e);\n    throw new IllegalStateException(\"Missing required dependency\", e);\n}","preventionTips":["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."],"tags":["java","android","null-pointer","validation"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}