{"record":{"id":"6b725189357980a2","repo":"TheAlgorithms/Java","slug":"number-of-variables-cannot-be-negative","errorCode":null,"errorMessage":"Number of variables cannot be negative.","messagePattern":"Number of variables cannot be negative\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java","lineNumber":96,"sourceCode":"    /** Stores one valid truth assignment for all variables (1-indexed). */\n    private final boolean[] variableAssignments;\n\n    /** Indicates whether a valid solution exists. */\n    private boolean hasSolution = true;\n\n    /** Tracks whether the {@code solve()} method has been called. */\n    private boolean isSolved = false;\n\n    /**\n     * Initializes the TwoSat solver with the given number of variables.\n     *\n     * @param numberOfVariables the number of boolean variables\n     * @throws IllegalArgumentException if the number of variables is negative\n     */\n    @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n    TwoSat(int numberOfVariables) {\n        if (numberOfVariables < 0) {\n            throw new IllegalArgumentException(\"Number of variables cannot be negative.\");\n        }\n        this.numberOfVariables = numberOfVariables;\n        int n = 2 * numberOfVariables + 1;\n\n        graph = (ArrayList<Integer>[]) new ArrayList[n];\n        graphTranspose = (ArrayList<Integer>[]) new ArrayList[n];\n        for (int i = 0; i < n; i++) {\n            graph[i] = new ArrayList<>();\n            graphTranspose[i] = new ArrayList<>();\n        }\n        variableAssignments = new boolean[numberOfVariables + 1];\n    }\n\n    /**\n     * Adds a clause of the form (a ∨ b) to the boolean expression.\n     *\n     * <p>\n     * Example: To add (¬x₁ ∨ x₂), call:","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java#L78-L114","documentation":"Thrown by the TwoSat constructor when `numberOfVariables` is negative. The solver allocates implication-graph arrays of size `2*numberOfVariables+1`, so a negative count would break indexing; the guard rejects it immediately. Zero variables is allowed (trivially satisfiable).","triggerScenarios":"Constructing `new TwoSat(numberOfVariables)` with a negative int — e.g. a count from an unchecked `input - 1` or a parse failure.","commonSituations":"Off-by-one in deriving the variable count; reading the count from untrusted input; arithmetic that underflows to negative.","solutions":["Validate that `numberOfVariables >= 0` at the call site","Fix the upstream computation producing the negative count","Default to 0 only when an empty formula is intended"],"exampleFix":"// before\nTwoSat ts = new TwoSat(n - 1);\n// after\nif (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nTwoSat ts = new TwoSat(n);","handlingStrategy":"validation","validationCode":"if (numberOfVariables < 0) {\n    throw new IllegalArgumentException(\"numberOfVariables must be >= 0\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    new TwoSat(n);\n} catch (IllegalArgumentException e) {\n    // handle negative count\n}","preventionTips":["Validate variable counts at the input boundary","Guard arithmetic that could underflow before passing to the constructor"],"tags":["sat","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}