{"record":{"id":"dc6f9e2a456572b6","repo":"TheAlgorithms/Java","slug":"please-call-solve-before-fetching-the-solution","errorCode":null,"errorMessage":"Please call solve() before fetching the solution.","messagePattern":"Please call solve\\(\\) before fetching the solution\\.","errorType":"panic","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java","lineNumber":214,"sourceCode":"     * @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        }\n        return variableAssignments.clone();\n    }\n\n    /** Performs DFS to compute topological order. */\n    private void dfsForTopologicalOrder(int u, boolean[] visited, Stack<Integer> topologicalOrder) {\n        visited[u] = true;\n        for (int v : graph[u]) {\n            if (!visited[v]) {\n                dfsForTopologicalOrder(v, visited, topologicalOrder);\n            }\n        }\n        topologicalOrder.push(u);\n    }\n","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java#L196-L232","documentation":"Thrown by `TwoSat.getSolutions` when `solve()` has not been called. The assignment array is only computed during `solve()`, so reading it earlier would return a default all-false array. Like the sibling check, this is an `Error` indicating API misuse.","triggerScenarios":"Calling `getSolutions()` without a prior `solve()`, or when `solve()` sits behind a branch that did not execute.","commonSituations":"Forgetting to call solve(); refactoring that drops the call; assuming construction implies solving.","solutions":["Call `solve()` before `getSolutions()`","Gate `getSolutions()` behind an `isSolutionExists()` check that itself requires solve()","Encapsulate the full sequence in a single facade method"],"exampleFix":"// before\nboolean[] s = ts.getSolutions(); // throws Error\n// after\nts.solve();\nboolean[] s = ts.getSolutions();","handlingStrategy":"validation","validationCode":"ts.solve(); // must run before getSolutions()","typeGuard":null,"tryCatchPattern":"try {\n    ts.getSolutions();\n} catch (Error e) {\n    // solve() not called first\n}","preventionTips":["Always call solve() before any getter","Catch Error (not just Exception) if you defend at the call site"],"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"}