{"record":{"id":"34c2826c1b0a4e00","repo":"TheAlgorithms/Java","slug":"numofterms-nonnegative","errorCode":null,"errorMessage":"numOfTerms nonnegative.","messagePattern":"numOfTerms nonnegative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java","lineNumber":27,"sourceCode":" *\n * <p>\n * Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression\n */\npublic final class SumOfArithmeticSeries {\n    private SumOfArithmeticSeries() {\n    }\n\n    /**\n     * Calculate sum of arithmetic series\n     *\n     * @param firstTerm the initial term of an arithmetic series\n     * @param commonDiff the common difference of an arithmetic series\n     * @param numOfTerms the total terms of an arithmetic series\n     * @return sum of given arithmetic series\n     */\n    public static double sumOfSeries(final double firstTerm, final double commonDiff, final int numOfTerms) {\n        if (numOfTerms < 0) {\n            throw new IllegalArgumentException(\"numOfTerms nonnegative.\");\n        }\n        return (numOfTerms / 2.0 * (2 * firstTerm + (numOfTerms - 1) * commonDiff));\n    }\n}\n","sourceCodeStart":9,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/SumOfArithmeticSeries.java#L9-L32","documentation":"Thrown by SumOfArithmeticSeries.sumOfSeries when numOfTerms is negative. The closed-form formula (n/2 * (2a + (n-1)d)) is mathematically defined for non-negative term counts; a negative count has no meaning as 'number of terms'. The message text ('numOfTerms nonnegative.') is a terse fragment rather than a full sentence.","triggerScenarios":"Call sumOfSeries(firstTerm, commonDiff, -1) or pass a term count derived from a subtraction/collection size that went negative.","commonSituations":"Miscomputing a length (e.g., end - start with start > end), off-by-one on a parsed count, or assuming a size method never returns negative.","solutions":["Ensure numOfTerms >= 0 before calling; treat 0 terms as sum 0 if that matches your domain.","Fix the upstream computation producing the negative count.","Consider contributing a clearer message upstream ('numOfTerms must be non-negative')."],"exampleFix":"// before\ndouble s = SumOfArithmeticSeries.sumOfSeries(a, d, terms);\n\n// after\nif (terms < 0) throw new IllegalArgumentException(\"terms must be >= 0, got \" + terms);\ndouble s = SumOfArithmeticSeries.sumOfSeries(a, d, terms);","handlingStrategy":"validation","validationCode":"if (numOfTerms < 0) throw new IllegalArgumentException(\"numOfTerms must be >= 0\");\ndouble s = SumOfArithmeticSeries.sumOfSeries(a, d, numOfTerms);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never derive a 'count of terms' from an unchecked subtraction.","Validate counts at the system boundary (CLI, HTTP, config) before they reach math utilities."],"tags":["math","series","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}