{"record":{"id":"00331f4146758b5a","repo":"TheAlgorithms/Java","slug":"please-call-solve-before-checking-for-a-solution","errorCode":null,"errorMessage":"Please call solve() before checking for a solution.","messagePattern":"Please call solve\\(\\) before checking for a solution\\.","errorType":"panic","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java","lineNumber":200,"sourceCode":"            int notI = negate(i);\n            if (component[i] == component[notI]) {\n                hasSolution = false;\n                return;\n            }\n            // If SCC(i) > SCC(¬i), then variable i is true.\n            variableAssignments[i] = component[i] > component[notI];\n        }\n    }\n\n    /**\n     * Returns whether the given boolean formula is satisfiable.\n     *\n     * @return {@code true} if a solution exists; {@code false} otherwise\n     * @throws Error if called before {@link #solve()}\n     */\n    boolean isSolutionExists() {\n        if (!isSolved) {\n            throw new Error(\"Please call solve() before checking for a solution.\");\n        }\n        return hasSolution;\n    }\n\n    /**\n     * Returns one valid assignment of variables that satisfies the boolean formula.\n     *\n     * @return a boolean array where {@code result[i]} represents the truth value of\n     *         variable {@code xᵢ}\n     * @throws Error if called before {@link #solve()} or if no solution exists\n     */\n    boolean[] getSolutions() {\n        if (!isSolved) {\n            throw new Error(\"Please call solve() before fetching the solution.\");\n        }\n        if (!hasSolution) {\n            throw new Error(\"No satisfying assignment exists for the given expression.\");\n        }","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java#L182-L218","documentation":"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.","triggerScenarios":"Calling `isSolutionExists()` before `solve()` — forgetting the `solve()` call or reordering the two statements.","commonSituations":"Copy-paste omission of `solve()`; refactoring that moved the solve call into a branch that was not taken; assuming the constructor solves.","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"],"exampleFix":"// before\nif (ts.isSolutionExists()) { ... } // throws Error\n// after\nts.solve();\nif (ts.isSolutionExists()) { ... }","handlingStrategy":"validation","validationCode":"ts.solve(); // always solve before querying satisfiability","typeGuard":null,"tryCatchPattern":"try {\n    ts.isSolutionExists();\n} catch (Error e) {\n    // solve() was not called first\n}","preventionTips":["Treat solve() as a mandatory precondition for every query method","Encapsulate the solve->query order in a single facade method"],"tags":["sat","state-machine","api-misuse"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}