{"record":{"id":"ac0886010c6d3a4b","repo":"jhy/jsoup","slug":"you-must-execute-the-request-before-getting-a-resp","errorCode":null,"errorMessage":"You must execute the request before getting a response.","messagePattern":"You must execute the request before getting a response\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/helper/HttpConnection.java","lineNumber":383,"sourceCode":"        res = Response.execute(req);\n        return res;\n    }\n\n    @Override\n    public Connection.Request request() {\n        return req;\n    }\n\n    @Override\n    public Connection request(Connection.Request request) {\n        req = (HttpConnection.Request) request; // will throw a class-cast exception if the user has extended some but not all of Connection; that's desired\n        return this;\n    }\n\n    @Override\n    public Connection.Response response() {\n        if (res == null) {\n            throw new IllegalArgumentException(\"You must execute the request before getting a response.\");\n        }\n        return res;\n    }\n\n    @Override\n    public Connection response(Connection.Response response) {\n        res = response;\n        return this;\n    }\n\n    @Override\n    public Connection postDataCharset(String charset) {\n        req.postDataCharset(charset);\n        return this;\n    }\n\n    @Override public Connection auth(@Nullable RequestAuthenticator authenticator) {\n        req.auth(authenticator);","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/helper/HttpConnection.java#L365-L401","documentation":"HttpConnection.Response is only populated after Connection.execute() (or get()/post()) completes. Calling response() on the Connection before executing throws this IllegalArgumentException because the internal `res` field is still null. It guards against reading a response that does not exist yet.","triggerScenarios":"Calling conn.response() after Jsoup.connect(url) but before conn.execute(); calling response() on a freshly built Connection; chaining that reads response before execute().","commonSituations":"Refactoring from one-shot Jsoup.connect(...).get() to multi-step execute()/response() flows; forgetting that response() is a getter on Connection, not an executor; reading response in a finally block before execute succeeded.","solutions":["Call Connection.execute() (or get()/post()) before response()","Use conn.execute().response() chained form so ordering is enforced","Prefer Jsoup.connect(url).execute() and hold the returned Response directly"],"exampleFix":"// before\nConnection conn = Jsoup.connect(url);\nDocument doc = conn.response().parse();\n// after\nConnection.Response res = Jsoup.connect(url).execute();\nDocument doc = res.parse();","handlingStrategy":"try-catch","validationCode":"if (connection instanceof HttpConnection) { /* prefer tracking your own executed flag */ }","typeGuard":null,"tryCatchPattern":"try { return conn.response(); } catch (IllegalArgumentException e) { return conn.execute(); }","preventionTips":["Always call execute() (or get()/post()) before reading response()","Keep a boolean 'executed' flag in wrappers around HttpConnection","Prefer the single-call forms Jsoup.connect(url).execute() / .get()"],"tags":["java","jsoup","http","api-misuse"],"backgroundTag":"invalid-state-transition","analyzedSha":"9851ac5d9c576c6888910b5a51a2362bbc978959","analyzedAt":"2026-09-08T15:22:04.931Z","contentChangedAt":"2026-09-08T15:22:04.931Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}