{"record":{"id":"cc028ba10681f07e","repo":"TheAlgorithms/Java","slug":"arguments-must-not-be-null","errorCode":null,"errorMessage":"Arguments must not be null","messagePattern":"Arguments must not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java","lineNumber":129,"sourceCode":"            if (result == 1L) {\n                return 1L; // early exit\n            }\n        }\n        return result;\n    }\n\n    /**\n     * BigInteger-backed gcd that works for the full integer range (and beyond).\n     * This is the recommended method when inputs may be Long.MIN_VALUE or when you\n     * need an exact result even if it is greater than Long.MAX_VALUE.\n     * @param a first value (may be negative)\n     * @param b second value (may be negative)\n     * @return non-negative gcd as a {@link BigInteger}\n     */\n    public static BigInteger gcdBig(BigInteger a, BigInteger b) {\n\n        if (a == null || b == null) {\n            throw new NullPointerException(\"Arguments must not be null\");\n        }\n        return a.abs().gcd(b.abs());\n    }\n\n    /**\n     * Convenience overload that accepts signed-64 inputs and returns BigInteger gcd.\n     */\n    public static BigInteger gcdBig(long a, long b) {\n        return gcdBig(BigInteger.valueOf(a), BigInteger.valueOf(b));\n    }\n\n    /**\n     * int overload for convenience.\n     */\n    public static int gcd(int a, int b) {\n        return (int) gcd((long) a, (long) b);\n    }\n}","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java#L111-L147","documentation":"Thrown by BitwiseGCD.gcdBig(BigInteger a, BigInteger b) when either argument is null. The BigInteger-backed gcd cannot compute a.abs() or b.abs() on a null reference, so the method fails fast with a NullPointerException rather than letting the NPE surface deeper inside BigInteger with a less clear trace. Pass non-null BigInteger instances (BigInteger.ZERO is valid).","triggerScenarios":"Calling `gcdBig(null, BigInteger.TEN)` or `gcdBig(BigInteger.ONE, null)`. The guard `a == null || b == null` triggers before any computation; null is the only invalid input (any BigInteger value, including ZERO, is accepted).","commonSituations":"BigInteger values loaded from a map/optional that returned null; unboxing of a boxed reference that was never set; chaining from a parser that yields null on malformed input.","solutions":["Ensure both arguments are non-null; substitute BigInteger.ZERO (or another sensible default) for missing values.","Use Objects.requireNonNull(a, 'a') at the call site for a clearer message.","Validate the source (Optional/map lookup) before calling."],"exampleFix":"// before\nBitwiseGCD.gcdBig(maybeNullA, maybeNullB);  // throws NPE\n\n// after\nBigInteger a = Objects.requireNonNullElse(maybeNullA, BigInteger.ZERO);\nBigInteger b = Objects.requireNonNullElse(maybeNullB, BigInteger.ZERO);\nBitwiseGCD.gcdBig(a, b);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(a, \"a\");\nObjects.requireNonNull(b, \"b\");\nBitwiseGCD.gcdBig(a, b);","typeGuard":"public static boolean nonNullArgs(BigInteger a, BigInteger b) {\n    return a != null && b != null;\n}","tryCatchPattern":"try {\n    return BitwiseGCD.gcdBig(a, b);\n} catch (NullPointerException e) {\n    return BitwiseGCD.gcdBig(\n        Objects.requireNonNullElse(a, BigInteger.ZERO),\n        Objects.requireNonNullElse(b, BigInteger.ZERO));\n}","preventionTips":["Use Objects.requireNonNull at the call site for a clear message.","Default missing values to BigInteger.ZERO where sensible.","Validate Optional/map lookups before passing to gcdBig."],"tags":["bit-manipulation","gcd","null-check","biginteger","nullpointerexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}