TheAlgorithms/Java · error · Error
Please call solve() before checking for a solution.
Error message
Please call solve() before checking for a solution.
What it means
Thrown by `TwoSat.isSolutionExists` when `solve()` has not yet been called. The solver defers all SCC computation to `solve()`, so `hasSolution` is meaningless before then. This is thrown as `Error` (not `IllegalArgumentException`), signalling API misuse rather than bad data.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java:200
int notI = negate(i);
if (component[i] == component[notI]) {
hasSolution = false;
return;
}
// If SCC(i) > SCC(¬i), then variable i is true.
variableAssignments[i] = component[i] > component[notI];
}
}
/**
* Returns whether the given boolean formula is satisfiable.
*
* @return {@code true} if a solution exists; {@code false} otherwise
* @throws Error if called before {@link #solve()}
*/
boolean isSolutionExists() {
if (!isSolved) {
throw new Error("Please call solve() before checking for a solution.");
}
return hasSolution;
}
/**
* Returns one valid assignment of variables that satisfies the boolean formula.
*
* @return a boolean array where {@code result[i]} represents the truth value of
* variable {@code xᵢ}
* @throws Error if called before {@link #solve()} or if no solution exists
*/
boolean[] getSolutions() {
if (!isSolved) {
throw new Error("Please call solve() before fetching the solution.");
}
if (!hasSolution) {
throw new Error("No satisfying assignment exists for the given expression.");
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Call `solve()` before `isSolutionExists()`
- Wrap the build->solve->query sequence in a helper so the order cannot be skipped
- Add a unit test that asserts the sequence
Example fix
// before
if (ts.isSolutionExists()) { ... } // throws Error
// after
ts.solve();
if (ts.isSolutionExists()) { ... } Defensive patterns
Strategy: validation
Validate before calling
ts.solve(); // always solve before querying satisfiability
Try / catch
try {
ts.isSolutionExists();
} catch (Error e) {
// solve() was not called first
} Prevention
- Treat solve() as a mandatory precondition for every query method
- Encapsulate the solve->query order in a single facade method
When it happens
Trigger: Calling `isSolutionExists()` before `solve()` — forgetting the `solve()` call or reordering the two statements.
Common situations: Copy-paste omission of `solve()`; refactoring that moved the solve call into a branch that was not taken; assuming the constructor solves.
Related errors
- Please call solve() before fetching the solution.
- Number of variables cannot be negative.
- Variable number must be between 1 and {numberOfVariables}
- No satisfying assignment exists for the given expression.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/00331f4146758b5a.
Report an issue: GitHub.