square/retrofit · error · NullPointerException
error == null
Error message
error == null
What it means
Identical contract to RxJava1's Result: `Result.error(Throwable)` at Result.java:26 requires a non-null cause so that an error Result always carries a real Throwable and `.error()` is safe to read. This guards the Result invariant (exactly one of response/error set).
Source
Thrown at retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/Result.java:26
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* 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;
}
/**View on GitHub (pinned to d0b112dad0)
Solutions
- Pass a concrete non-null Throwable: `Result.error(new IOException("x"))`.
- Guard nullable sources: `Result.error(t != null ? t : new IllegalStateException())`.
- Prefer the built-in RxJava2CallAdapter to build Result objects.
Example fix
// before
Result<User> r = Result.error(getErrorOrNull()); // NPE if null
// after
Throwable t = getErrorOrNull();
Result<User> r = Result.error(t != null ? t : new IllegalStateException("no cause")); Defensive patterns
Strategy: validation
Validate before calling
static <T> Result<T> safeError(Throwable t) {
if (t == null) throw new IllegalArgumentException("error must not be null");
return Result.error(t);
} Try / catch
try {
Result<User> r = Result.error(maybeNullThrowable);
} catch (NullPointerException e) {
if (e.getMessage().equals("error == null")) {
Result<User> r = Result.error(new IllegalStateException("unknown error"));
} else throw e;
} Prevention
- Never pass a nullable into Result.error().
- Let the adapter build Result; avoid manual construction.
- Always supply a real exception in tests.
When it happens
Trigger: Calling `Result.error(null)` in app code, a custom adapter, a converter, or a test fixture.
Common situations: Hand-building Result instances in custom adapters or tests; passing a nullable cause expression.
Related errors
AI-assisted analysis of square/retrofit@d0b112dad0 (2026-08-04).
Data as JSON: /data/errors/026968d47a6cfc3f.json.
Report an issue: GitHub.