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. A response-bearing Result must carry a real Response so callers can read body/status.

Source

Thrown at retrofit-adapters/rxjava3/src/main/java/retrofit2/adapter/rxjava3/Result.java:32

 * limitations under the License.
 */
package retrofit2.adapter.rxjava3;

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

  1. Pass a non-null Response: `Result.response(Response.success(body))`.
  2. If you only have an error, use `Result.error(throwable)`.
  3. 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

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.

Related errors


AI-assisted analysis of square/retrofit@d0b112dad0 (2026-08-04). Data as JSON: /data/errors/221a66eca7062e41.json. Report an issue: GitHub.