TheAlgorithms/Java · error · IllegalArgumentException
n must be a non-negative integer
Error message
n must be a non-negative integer
What it means
Thrown by FibonacciSeries.fibonacci(n) when n < 0. The Fibonacci sequence is indexed from 0, and the naive recursion fibonacci(n-1)+fibonacci(n-2) has no base case for negatives, so the guard rejects them. n <= 1 returns n (base cases). Note: exponential time O(2^n) — large n is slow, not erroneous.
Source
Thrown at src/main/java/com/thealgorithms/recursion/FibonacciSeries.java:36
* <li>{@link com.thealgorithms.maths.FibonacciNumberCheck} - Utility to check if a given number is a Fibonacci number</li>
* <li>{@link com.thealgorithms.matrix.matrixexponentiation.Fibonacci} - O(log n) Matrix Exponentiation approach</li>
* </ul>
*/
public final class FibonacciSeries {
private FibonacciSeries() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Calculates the nth term in the Fibonacci sequence using recursion.
*
* @param n the position in the Fibonacci sequence (must be non-negative)
* @return the nth Fibonacci number
* @throws IllegalArgumentException if n is negative
*/
public static int fibonacci(int n) {
if (n < 0) {
throw new IllegalArgumentException("n must be a non-negative integer");
}
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate n >= 0 at the caller.
- For repeated/large queries, memoize or use an iterative/matrix approach instead of naive recursion.
- Guard n <= 46 if using int return type to avoid silent overflow (fib(47) overflows int).
Example fix
// before
int f = FibonacciSeries.fibonacci(n); // n may be negative
// after
if (n < 0) throw new IllegalArgumentException("n must be >= 0");
int f = FibonacciSeries.fibonacci(n); Defensive patterns
Strategy: validation
Validate before calling
if (n < 0) {
throw new IllegalArgumentException("n must be >= 0");
}
int f = FibonacciSeries.fibonacci(n); Type guard
static boolean validFibIndex(int n) {
return n >= 0;
} Try / catch
try {
int f = FibonacciSeries.fibonacci(n);
} catch (IllegalArgumentException e) {
logger.warn("Negative Fibonacci index: {}", n);
} Prevention
- Range-check n at the input boundary.
- Guard n <= 46 to avoid silent int overflow (fib(47) overflows int).
- For repeated or large queries, replace naive recursion with memoization or an iterative method.
When it happens
Trigger: Call fibonacci(-1) or any negative n. Very large positive n (e.g. 50+) does not throw but is extremely slow and will overflow int around n=47.
Common situations: Computing n from a decremented counter that went negative; parsed index not validated; off-by-one in a loop bound feeding fibonacci(i-1) when i could be 0.
Related errors
- number is negative
- Number of discs must be non-negative
- Target must be non-negative
- Input n must be non-negative
- Input must be non-negative. Received:
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/73dc18b71079b448.
Report an issue: GitHub.