TheAlgorithms/Java · error · IllegalArgumentException
Input index cannot be null or negative!
Error message
Input index cannot be null or negative!
What it means
Thrown by FibonacciJavaStreams.calculate when index is null or compareTo(ZERO) < 0. The method computes the nth Fibonacci number via a BigDecimal stream reduction; a null index would NPE inside the comparator, and a negative index has no Fibonacci value. The guard combines both into a single message, so the same error text covers two distinct failure modes.
Source
Thrown at src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java:32
* <ul>
* <li>{@link com.thealgorithms.maths.FibonacciLoop} - Standard Iterative (Loop) approach</li>
* <li>{@link com.thealgorithms.recursion.FibonacciSeries} - Naive Recursive approach</li>
* <li>{@link com.thealgorithms.dynamicprogramming.Fibonacci} - Dynamic Programming approaches (Memoization, Bottom-Up, Optimized)</li>
* <li>{@link com.thealgorithms.maths.FibonacciNumberGoldenRation} - Closed-form expression using Binet's formula</li>
* <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>
* * @author caos321
* @date 14 October 2021 (Thursday)
*/
public final class FibonacciJavaStreams {
private FibonacciJavaStreams() {
}
public static Optional<BigDecimal> calculate(final BigDecimal index) {
if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("Input index cannot be null or negative!");
}
if (index.compareTo(BigDecimal.ONE) < 0) {
return Optional.of(BigDecimal.ZERO);
}
if (index.compareTo(BigDecimal.TWO) < 0) {
return Optional.of(BigDecimal.ONE);
}
final List<BigDecimal> results = Stream.iterate(index, x -> x.compareTo(BigDecimal.ZERO) > 0, x -> x.subtract(BigDecimal.ONE))
.reduce(List.of(), (list, current) -> list.isEmpty() || list.size() < 2 ? List.of(BigDecimal.ZERO, BigDecimal.ONE) : List.of(list.get(1), list.get(0).add(list.get(1))), (list1, list2) -> list1);
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(results.size() - 1));
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a non-null, non-negative BigDecimal such as calculate(BigDecimal.TEN).
- If using Optional, unwrap safely: opt.orElseThrow() after a non-negative check.
- Validate index != null && index.signum() >= 0 before calling.
Example fix
// before Optional<BigDecimal> f = FibonacciJavaStreams.calculate(null); // after Optional<BigDecimal> f = FibonacciJavaStreams.calculate(BigDecimal.valueOf(10));
Defensive patterns
Strategy: validation
Validate before calling
if (index == null || index.signum() < 0) {
throw new IllegalArgumentException("Fibonacci index must be non-null and >= 0");
}
FibonacciJavaStreams.calculate(index); Type guard
static boolean isValidFibIndex(BigDecimal i) {
return i != null && i.signum() >= 0;
} Prevention
- Unwrap Optional<BigDecimal> and null-check before calling.
- Validate signum() >= 0 for parsed indices.
- Avoid defaulting index fields to null.
When it happens
Trigger: Calling calculate(null) or calculate(BigDecimal.valueOf(-1)). The check fires before the early-return guards for index < 1 and index < 2, so a null or negative always hits this branch first.
Common situations: Optional/nullable BigDecimal from a parser not unwrapped; index read from input that accepted a minus sign; arithmetic producing a negative index; default null field passed.
Related errors
- Input 'n' must be a non-negative integer.
- Input 'n' must be a non-negative integer.
- Input must contain only '0' and '1'.
- Inputs cannot be null.
- Key must not be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/8a127f8c6f95d226.
Report an issue: GitHub.