TheAlgorithms/Java · error · NullPointerException
Arguments must not be null
Error message
Arguments must not be null
What it means
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).
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/BitwiseGCD.java:129
if (result == 1L) {
return 1L; // early exit
}
}
return result;
}
/**
* BigInteger-backed gcd that works for the full integer range (and beyond).
* This is the recommended method when inputs may be Long.MIN_VALUE or when you
* need an exact result even if it is greater than Long.MAX_VALUE.
* @param a first value (may be negative)
* @param b second value (may be negative)
* @return non-negative gcd as a {@link BigInteger}
*/
public static BigInteger gcdBig(BigInteger a, BigInteger b) {
if (a == null || b == null) {
throw new NullPointerException("Arguments must not be null");
}
return a.abs().gcd(b.abs());
}
/**
* Convenience overload that accepts signed-64 inputs and returns BigInteger gcd.
*/
public static BigInteger gcdBig(long a, long b) {
return gcdBig(BigInteger.valueOf(a), BigInteger.valueOf(b));
}
/**
* int overload for convenience.
*/
public static int gcd(int a, int b) {
return (int) gcd((long) a, (long) b);
}
}View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before BitwiseGCD.gcdBig(maybeNullA, maybeNullB); // throws NPE // after BigInteger a = Objects.requireNonNullElse(maybeNullA, BigInteger.ZERO); BigInteger b = Objects.requireNonNullElse(maybeNullB, BigInteger.ZERO); BitwiseGCD.gcdBig(a, b);
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(a, "a"); Objects.requireNonNull(b, "b"); BitwiseGCD.gcdBig(a, b);
Type guard
public static boolean nonNullArgs(BigInteger a, BigInteger b) {
return a != null && b != null;
} Try / catch
try {
return BitwiseGCD.gcdBig(a, b);
} catch (NullPointerException e) {
return BitwiseGCD.gcdBig(
Objects.requireNonNullElse(a, BigInteger.ZERO),
Objects.requireNonNullElse(b, BigInteger.ZERO));
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Absolute value of Long.MIN_VALUE does not fit into signed lo
- Maze must not be null or empty.
- Invalid BCD digit: {}
- Value out of bounds for BCD representation: {}
- Shift amount cannot be negative: {}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/cc028ba10681f07e.
Report an issue: GitHub.