{"record":{"id":"8a127f8c6f95d226","repo":"TheAlgorithms/Java","slug":"input-index-cannot-be-null-or-negative","errorCode":null,"errorMessage":"Input index cannot be null or negative!","messagePattern":"Input index cannot be null or negative!","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java","lineNumber":32,"sourceCode":" * <ul>\n * <li>{@link com.thealgorithms.maths.FibonacciLoop} - Standard Iterative (Loop) approach</li>\n * <li>{@link com.thealgorithms.recursion.FibonacciSeries} - Naive Recursive approach</li>\n * <li>{@link com.thealgorithms.dynamicprogramming.Fibonacci} - Dynamic Programming approaches (Memoization, Bottom-Up, Optimized)</li>\n * <li>{@link com.thealgorithms.maths.FibonacciNumberGoldenRation} - Closed-form expression using Binet's formula</li>\n * <li>{@link com.thealgorithms.maths.FibonacciNumberCheck} - Utility to check if a given number is a Fibonacci number</li>\n * <li>{@link com.thealgorithms.matrix.matrixexponentiation.Fibonacci} - O(log n) Matrix Exponentiation approach</li>\n * </ul>\n * * @author caos321\n * @date 14 October 2021 (Thursday)\n */\n\npublic final class FibonacciJavaStreams {\n    private FibonacciJavaStreams() {\n    }\n\n    public static Optional<BigDecimal> calculate(final BigDecimal index) {\n        if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {\n            throw new IllegalArgumentException(\"Input index cannot be null or negative!\");\n        }\n\n        if (index.compareTo(BigDecimal.ONE) < 0) {\n            return Optional.of(BigDecimal.ZERO);\n        }\n\n        if (index.compareTo(BigDecimal.TWO) < 0) {\n            return Optional.of(BigDecimal.ONE);\n        }\n\n        final List<BigDecimal> results = Stream.iterate(index, x -> x.compareTo(BigDecimal.ZERO) > 0, x -> x.subtract(BigDecimal.ONE))\n                                             .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);\n\n        return results.isEmpty() ? Optional.empty() : Optional.of(results.get(results.size() - 1));\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java#L14-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nOptional<BigDecimal> f = FibonacciJavaStreams.calculate(null);\n\n// after\nOptional<BigDecimal> f = FibonacciJavaStreams.calculate(BigDecimal.valueOf(10));","handlingStrategy":"validation","validationCode":"if (index == null || index.signum() < 0) {\n    throw new IllegalArgumentException(\"Fibonacci index must be non-null and >= 0\");\n}\nFibonacciJavaStreams.calculate(index);","typeGuard":"static boolean isValidFibIndex(BigDecimal i) {\n    return i != null && i.signum() >= 0;\n}","tryCatchPattern":null,"preventionTips":["Unwrap Optional<BigDecimal> and null-check before calling.","Validate signum() >= 0 for parsed indices.","Avoid defaulting index fields to null."],"tags":["validation","null-safety","sequences","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}