{"id":"4d343c96f4c8275a","repo":"square/retrofit","slug":"response-null-4d343c","errorCode":null,"errorMessage":"response == null","messagePattern":"response == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/Result.java","lineNumber":32,"sourceCode":" * limitations under the License.\n */\npackage retrofit2.adapter.rxjava2;\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/rxjava2/src/main/java/retrofit2/adapter/rxjava2/Result.java#L14-L50","documentation":"`Result.response(Response<T>)` at Result.java:32 rejects null to keep the Result invariant intact (exactly one of response/error set). A response-bearing Result must carry a real Response.","triggerScenarios":"Calling `Result.response(null)` in app code, a custom adapter, a converter, or a test.","commonSituations":"Hand-rolling Result instances; passing a possibly-null Response from a custom flow.","solutions":["Pass a non-null Response: `Result.response(Response.success(body))`.","If you only have an error, use `Result.error(throwable)` instead.","Delegate Result construction to the built-in adapter."],"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":"try {\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 only a failure exists.","Avoid manual Result construction."],"tags":["retrofit","rxjava2","java","null-guard","result"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}