{"id":"07f7ab75e3045039","repo":"square/retrofit","slug":"error-null","errorCode":null,"errorMessage":"error == null","messagePattern":"error == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/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.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  /**","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/Result.java#L8-L44","documentation":"`Result<T>` is a value object representing the outcome of one HTTP call -- it holds either a Response or an error, never both, never neither. The factory method `Result.error(Throwable)` at Result.java:26 rejects a null throwable to preserve that invariant: an error Result must carry a real cause so downstream code (and `.error()`) can rely on it.","triggerScenarios":"Calling `Result.error(null)` directly in application code, a custom call adapter, a converter, or a test fixture. The null check fires immediately at Result.java:26.","commonSituations":"Writing a custom adapter that wraps failures and passing a null cause when a sub-call returned null; building Result instances by hand in tests; or chaining `Result.error(something_that_might_be_null)` without guarding.","solutions":["Pass a non-null Throwable, e.g. `Result.error(new IOException(\"boom\"))`.","If the source can legitimately be null, supply a sentinel/default exception: `Result.error(t != null ? t : new IllegalStateException(\"unknown\"))`.","Prefer letting the built-in RxJavaCallAdapter construct Result instances rather than building them yourself."],"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":"// Validate before constructing:\nstatic <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":"// If the throwable may be null, guard at the call site:\ntry {\n    Result<User> r = Result.error(maybeNullThrowable);\n} catch (NullPointerException e) {\n    if (e.getMessage().equals(\"error == null\")) {\n        // supply a default cause or log and skip\n        Result<User> r = Result.error(new IllegalStateException(\"unknown error\"));\n    } else throw e;\n}","preventionTips":["Never pass a nullable expression into Result.error().","Let the RxJavaCallAdapter build Result objects; do not hand-roll them.","When writing tests, always supply a concrete exception."],"tags":["retrofit","rxjava","java","null-guard","result"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}