square/retrofit · error · NullPointerException
response == null
Error message
response == null
What it means
`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.
Source
Thrown at retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/Result.java:32
* limitations under the License.
*/
package retrofit2.adapter.rxjava2;
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: `Result.response(Response.success(body))`.
- If you only have an error, use `Result.error(throwable)` instead.
- Delegate Result construction to the built-in adapter.
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
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 only a failure exists.
- Avoid manual Result construction.
When it happens
Trigger: Calling `Result.response(null)` in app code, a custom adapter, a converter, or a test.
Common situations: Hand-rolling Result instances; passing a possibly-null Response from a custom flow.
Related errors
AI-assisted analysis of square/retrofit@d0b112dad0 (2026-08-04).
Data as JSON: /data/errors/4d343c96f4c8275a.json.
Report an issue: GitHub.