{"id":"942fa7f3fb9a24f8","repo":"square/retrofit","slug":"response-null","errorCode":null,"errorMessage":"response == null","messagePattern":"response == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/Result.java","lineNumber":32,"sourceCode":" * limitations under the License.\n */\npackage retrofit2.adapter.rxjava;\n\nimport java.io.IOException;\nimport javax.annotation.Nullable;\nimport retrofit2.Response;\n\n/** The result of executing an HTTP request. */\npublic final class Result<T> {\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static <T> Result<T> error(Throwable error) {\n    if (error == null) throw new NullPointerException(\"error == null\");\n    return new Result<>(null, error);\n  }\n\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static <T> Result<T> response(Response<T> response) {\n    if (response == null) throw new NullPointerException(\"response == null\");\n    return new Result<>(response, null);\n  }\n\n  private final @Nullable Response<T> response;\n  private final @Nullable Throwable error;\n\n  private Result(@Nullable Response<T> response, @Nullable Throwable error) {\n    this.response = response;\n    this.error = error;\n  }\n\n  /**\n   * The response received from executing an HTTP request. Only present when {@link #isError()} is\n   * false, null otherwise.\n   */\n  public @Nullable Response<T> response() {\n    return response;\n  }","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/Result.java#L14-L50","documentation":"`Result.response(Response<T>)` at Result.java:32 rejects a null response to keep Result's invariant intact (exactly one of response/error is set). A response-bearing Result must carry a real Response so callers can read the body/status.","triggerScenarios":"Calling `Result.response(null)` in app code, a custom adapter, a converter, or a test. The null check at line 32 fires immediately.","commonSituations":"Building Result instances manually; a custom adapter that wraps a possibly-null Response; tests that pass null when no mock was set up.","solutions":["Pass a non-null Response, e.g. `Result.response(Response.success(body))`.","If you genuinely have no response (only an error), use `Result.error(throwable)` instead.","Delegate Result construction to the built-in adapter to avoid hand-rolling."],"exampleFix":"// before\nResult<User> r = Result.response(maybeNullResponse); // NPE if null\n\n// after\nResult<User> r = resp != null\n    ? Result.response(resp)\n    : Result.error(new IllegalStateException(\"no response\"));","handlingStrategy":"validation","validationCode":"static <T> Result<T> safeResponse(retrofit2.Response<T> r) {\n    if (r == null) throw new IllegalArgumentException(\"response must not be null\");\n    return Result.response(r);\n}","typeGuard":null,"tryCatchPattern":"// Guard at the boundary if Response may be null:\ntry {\n    Result<User> r = Result.response(maybeNullResponse);\n} catch (NullPointerException e) {\n    if (e.getMessage().equals(\"response == null\")) {\n        Result<User> r = Result.error(new IllegalStateException(\"no response\"));\n    } else throw e;\n}","preventionTips":["Do not pass nullable values into Result.response().","Use Result.error() when you only have a failure.","Let the adapter build Result; avoid manual construction."],"tags":["retrofit","rxjava","java","null-guard","result"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}