TheAlgorithms/Java · error · IllegalArgumentException

Number of variables cannot be negative.

Error message

Number of variables cannot be negative.

What it means

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).

Source

Thrown at src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java:96

    /** Stores one valid truth assignment for all variables (1-indexed). */
    private final boolean[] variableAssignments;

    /** Indicates whether a valid solution exists. */
    private boolean hasSolution = true;

    /** Tracks whether the {@code solve()} method has been called. */
    private boolean isSolved = false;

    /**
     * Initializes the TwoSat solver with the given number of variables.
     *
     * @param numberOfVariables the number of boolean variables
     * @throws IllegalArgumentException if the number of variables is negative
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    TwoSat(int numberOfVariables) {
        if (numberOfVariables < 0) {
            throw new IllegalArgumentException("Number of variables cannot be negative.");
        }
        this.numberOfVariables = numberOfVariables;
        int n = 2 * numberOfVariables + 1;

        graph = (ArrayList<Integer>[]) new ArrayList[n];
        graphTranspose = (ArrayList<Integer>[]) new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
            graphTranspose[i] = new ArrayList<>();
        }
        variableAssignments = new boolean[numberOfVariables + 1];
    }

    /**
     * Adds a clause of the form (a ∨ b) to the boolean expression.
     *
     * <p>
     * Example: To add (¬x₁ ∨ x₂), call:

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that `numberOfVariables >= 0` at the call site
  2. Fix the upstream computation producing the negative count
  3. Default to 0 only when an empty formula is intended

Example fix

// before
TwoSat ts = new TwoSat(n - 1);
// after
if (n < 0) throw new IllegalArgumentException("n must be >= 0");
TwoSat ts = new TwoSat(n);
Defensive patterns

Strategy: validation

Validate before calling

if (numberOfVariables < 0) {
    throw new IllegalArgumentException("numberOfVariables must be >= 0");
}

Try / catch

try {
    new TwoSat(n);
} catch (IllegalArgumentException e) {
    // handle negative count
}

Prevention

When it happens

Trigger: Constructing `new TwoSat(numberOfVariables)` with a negative int — e.g. a count from an unchecked `input - 1` or a parse failure.

Common situations: Off-by-one in deriving the variable count; reading the count from untrusted input; arithmetic that underflows to negative.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/6b725189357980a2. Report an issue: GitHub.