TheAlgorithms/Java · error · IllegalArgumentException
n must be non-negative
Error message
n must be non-negative
What it means
Thrown by BellNumbers.compute(int n) when n is negative. Bell numbers B(n) are defined for n >= 0; a negative index has no combinatorial meaning. The method uses a Bell Triangle stored in a 2D array of size (n+1) x (n+1), so a negative n would cause a NegativeArraySizeException if not guarded — this check prevents that.
Source
Thrown at src/main/java/com/thealgorithms/maths/BellNumbers.java:31
*
* @author Chahat Sandhu, <a href="https://github.com/singhc7">singhc7</a>
* @see <a href="https://en.wikipedia.org/wiki/Bell_number">Bell Number (Wikipedia)</a>
*/
public final class BellNumbers {
private BellNumbers() {
}
/**
* Calculates the n-th Bell number using the Bell Triangle.
*
* @param n the index of the Bell number (must be non-negative)
* @return the n-th Bell number
* @throws IllegalArgumentException if n is negative or n > 25
*/
public static long compute(int n) {
if (n < 0) {
throw new IllegalArgumentException("n must be non-negative");
}
if (n == 0) {
return 1;
}
if (n > 25) {
throw new IllegalArgumentException("n must be <= 25. For larger n, use BigInteger implementation.");
}
// We use a 2D array to visualize the Bell Triangle
long[][] bellTriangle = new long[n + 1][n + 1];
// Base case: The triangle starts with 1
bellTriangle[0][0] = 1;
for (int i = 1; i <= n; i++) {
// Rule 1: The first number in a new row is the LAST number of the previous row
bellTriangle[i][0] = bellTriangle[i - 1][i - 1];
View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate n >= 0 before calling BellNumbers.compute(n).
- Check loop boundaries and arithmetic that computes n to ensure it cannot go negative.
- Add input validation at the data boundary to reject negative indices before they reach the computation.
Example fix
// before
BellNumbers.compute(-1); // throws 'n must be non-negative'
// after
if (n >= 0) {
long bell = BellNumbers.compute(n);
} else {
throw new IllegalArgumentException("Bell number index must be >= 0, got: " + n);
} Defensive patterns
Strategy: validation
Validate before calling
// Validate n before calling BellNumbers.compute
if (n < 0) {
throw new IllegalArgumentException("Bell number index must be >= 0, got: " + n);
}
if (n > 25) {
// use BigInteger implementation instead
throw new UnsupportedOperationException("Use BigInteger implementation for n > 25");
}
long bell = BellNumbers.compute(n); Type guard
static boolean isValidBellIndex(int n) {
return n >= 0 && n <= 25;
} Prevention
- Validate n >= 0 AND n <= 25 in a single guard before calling BellNumbers.compute.
- Check loop/recurrence boundaries to prevent index underflow.
- Cap user-supplied indices at the input boundary.
When it happens
Trigger: Calling BellNumbers.compute(-1), BellNumbers.compute(-100), or passing a computed index that underflows to negative (e.g., compute(someValue - offset) where offset > someValue).
Common situations: An off-by-one error in a loop boundary (e.g., iterating from n-1 down to 0 but calling compute(i-1)). A user-supplied index parsed from input without range validation. A subtraction that produces a negative result passed directly as n.
Related errors
- n must be <= 25. For larger n, use BigInteger implementation
- Index must be non-negative
- Invalid unit '{}'. Supported units are: {}
- inputUnit must be different from outputUnit.
- NULL_INPUT
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/85442a0802452824.
Report an issue: GitHub.