{"id":"09f4d3b89e901dad","repo":"square/retrofit","slug":"error-null-09f4d3","errorCode":null,"errorMessage":"error == null","messagePattern":"error == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava3/src/main/java/retrofit2/adapter/rxjava3/Result.java","lineNumber":26,"sourceCode":" *      http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\npackage retrofit2.adapter.rxjava3;\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  /**","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava3/src/main/java/retrofit2/adapter/rxjava3/Result.java#L8-L44","documentation":"`Result.error(Throwable)` at Result.java:26 requires a non-null cause to preserve Result's invariant that exactly one of response/error is set. An error Result must carry a real Throwable so `.error()` callers can rely on it.","triggerScenarios":"Calling `Result.error(null)` in app code, a custom adapter, a converter, or a test fixture.","commonSituations":"Hand-building Result instances; passing a nullable cause expression from a custom flow.","solutions":["Pass a concrete non-null Throwable: `Result.error(new IOException(\"x\"))`.","Guard nullable sources: `Result.error(t != null ? t : new IllegalStateException())`.","Let the built-in RxJava3CallAdapter construct Result objects."],"exampleFix":"// before\nResult<User> r = Result.error(getErrorOrNull()); // NPE if null\n\n// after\nThrowable t = getErrorOrNull();\nResult<User> r = Result.error(t != null ? t : new IllegalStateException(\"no cause\"));","handlingStrategy":"validation","validationCode":"static <T> Result<T> safeError(Throwable t) {\n    if (t == null) throw new IllegalArgumentException(\"error must not be null\");\n    return Result.error(t);\n}","typeGuard":null,"tryCatchPattern":"try {\n    Result<User> r = Result.error(maybeNullThrowable);\n} catch (NullPointerException e) {\n    if (e.getMessage().equals(\"error == null\")) {\n        Result<User> r = Result.error(new IllegalStateException(\"unknown error\"));\n    } else throw e;\n}","preventionTips":["Never pass a nullable into Result.error().","Avoid manual Result construction.","Always supply a real exception in tests."],"tags":["retrofit","rxjava3","java","null-guard","result"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}