square/retrofit · error · NullPointerException
response == null
Error message
response == null
What it means
`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.
Source
Thrown at retrofit-adapters/rxjava/src/main/java/retrofit2/adapter/rxjava/Result.java:32
* limitations under the License.
*/
package retrofit2.adapter.rxjava;
import java.io.IOException;
import javax.annotation.Nullable;
import retrofit2.Response;
/** The result of executing an HTTP request. */
public final class Result<T> {
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static <T> Result<T> error(Throwable error) {
if (error == null) throw new NullPointerException("error == null");
return new Result<>(null, error);
}
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static <T> Result<T> response(Response<T> response) {
if (response == null) throw new NullPointerException("response == null");
return new Result<>(response, null);
}
private final @Nullable Response<T> response;
private final @Nullable Throwable error;
private Result(@Nullable Response<T> response, @Nullable Throwable error) {
this.response = response;
this.error = error;
}
/**
* The response received from executing an HTTP request. Only present when {@link #isError()} is
* false, null otherwise.
*/
public @Nullable Response<T> response() {
return response;
}View on GitHub (pinned to d0b112dad0)
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.
Example fix
// before
Result<User> r = Result.response(maybeNullResponse); // NPE if null
// after
Result<User> r = resp != null
? Result.response(resp)
: Result.error(new IllegalStateException("no response")); Defensive patterns
Strategy: validation
Validate before calling
static <T> Result<T> safeResponse(retrofit2.Response<T> r) {
if (r == null) throw new IllegalArgumentException("response must not be null");
return Result.response(r);
} Try / catch
// Guard at the boundary if Response may be null:
try {
Result<User> r = Result.response(maybeNullResponse);
} catch (NullPointerException e) {
if (e.getMessage().equals("response == null")) {
Result<User> r = Result.error(new IllegalStateException("no response"));
} else throw e;
} Prevention
- 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.
When it happens
Trigger: Calling `Result.response(null)` in app code, a custom adapter, a converter, or a test. The null check at line 32 fires immediately.
Common situations: Building Result instances manually; a custom adapter that wraps a possibly-null Response; tests that pass null when no mock was set up.
Related errors
AI-assisted analysis of square/retrofit@d0b112dad0 (2026-08-04).
Data as JSON: /data/errors/942fa7f3fb9a24f8.json.
Report an issue: GitHub.