{"record":{"id":"bd25d2957349527f","repo":"lysine-dev/retrofit","slug":"response-null-bd25d2","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/lysine-dev/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/Result.java#L14-L50","documentation":"Result.response(Response) is the public factory for building a success-valued Result, used by the rxjava2 adapter to wrap a completed HTTP response inside a reactive stream as data. It rejects null because a Result must carry a definite response or a definite error — a null response would make the Result empty and break the response()/error() accessor invariants. It throws NullPointerException ('response == null') behind a ConstantConditions suppression.","triggerScenarios":"Calling Result.response(null) — typically in custom operators/transformers or tests that re-wrap a Response reference that resolved to null, or when mapping an empty cache hit. Thrown immediately at the top of Result.response(Response).","commonSituations":"Writing custom RxJava operators that wrap Response values; mock/test setup; cache layers returning null on miss that are then wrapped unconditionally.","solutions":["Null-check the argument first: `if (resp != null) Result.response(resp) else ...`.","If the call should represent a failure, use Result.error(...) instead.","Fix the upstream source so it never hands a null Response to the wrapper."],"exampleFix":"// before\nResult<User> r = Result.response(cachedResponse);\n// after\nif (cachedResponse == null) {\n  return Result.error(new IllegalStateException(\"no cached response\"));\n}\nResult<User> r = Result.response(cachedResponse);","handlingStrategy":"validation","validationCode":"Response<User> resp = executeOrCache();\nif (resp == null) throw new IllegalStateException(\"no response available\");\nResult<User> result = Result.response(resp);","typeGuard":null,"tryCatchPattern":"try {\n  Result<User> r = Result.response(resp);\n} catch (NullPointerException e) {\n  // resp was null; treat as an error Result instead\n  return Result.error(new IllegalStateException(\"null response\", e));\n}","preventionTips":["Use Objects.requireNonNull(resp, \"response\") before Result.response.","Never cache null as a sentinel for 'no value'; use Optional or an explicit error Result.","In tests, always pass a real retrofit2.Response instance."],"tags":["retrofit","rxjava2","null-safety","java","api-contract","response"],"backgroundTag":null,"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-13T23:49:47.955Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}