didi/DoKit · error · IllegalArgumentException

Context must not be null.

Error message

Context must not be null.

What it means

DokitPicasso.Builder's constructor requires a non-null Context (IllegalArgumentException('Context must not be null.') otherwise) because the whole image pipeline — cache, resource decoding, dispatcher — derives from it. It immediately stores context.getApplicationContext(), which also fails fast if a non-null but broken Context wrapper is passed. This is the earliest possible failure in Picasso setup, which is why it is guarded first.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/DokitPicasso.java:702

  /** Fluent API for creating {@link DokitPicasso} instances. */
  @SuppressWarnings("UnusedDeclaration") // Public API.
  public static class Builder {
    private final Context context;
    private Downloader downloader;
    private ExecutorService service;
    private Cache cache;
    private Listener listener;
    private RequestTransformer transformer;
    private List<RequestHandler> requestHandlers;
    private Bitmap.Config defaultBitmapConfig;

    private boolean indicatorsEnabled;
    private boolean loggingEnabled;

    /** Start building a new {@link DokitPicasso} instance. */
    public Builder(Context context) {
      if (context == null) {
        throw new IllegalArgumentException("Context must not be null.");
      }
      this.context = context.getApplicationContext();
    }

    /**
     * Specify the default {@link Bitmap.Config} used when decoding images. This can be overridden
     * on a per-request basis using {@link RequestCreator#config(Bitmap.Config) config(..)}.
     */
    public Builder defaultBitmapConfig(Bitmap.Config bitmapConfig) {
      if (bitmapConfig == null) {
        throw new IllegalArgumentException("Bitmap config must not be null.");
      }
      this.defaultBitmapConfig = bitmapConfig;
      return this;
    }

    /** Specify the {@link Downloader} that will be used for downloading images. */
    public Builder downloader(Downloader downloader) {

View on GitHub (pinned to 626827cddb)

Solutions

  1. Defer building until the context is real: fragment onAttach / Activity onCreate
  2. Use context.getApplicationContext() explicitly when wiring from a short-lived object
  3. In DI (Hilt/Dagger), make the Context provider @NonNull and fail at graph validation

Example fix

// before
public AvatarLoader(Context ctx) {
  this.picasso = new DokitPicasso.Builder(ctx).build(); // ctx null in ctor
}

// after
public AvatarLoader(Context ctx) {
  this.picasso = new DokitPicasso.Builder(
      Objects.requireNonNull(ctx, "context").getApplicationContext()).build();
}
Defensive patterns

Strategy: validation

Validate before calling

if (context != null) {
  DokitPicasso picasso = new DokitPicasso.Builder(context.getApplicationContext()).build();
}

Type guard

java.util.Objects.requireNonNull;

Try / catch

try {
  picasso = new DokitPicasso.Builder(context).build();
} catch (IllegalArgumentException e) {
  if ("Context must not be null.".equals(e.getMessage())) {
    throw new IllegalStateException("Picasso built before context attached", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: new DokitPicasso.Builder(null) from code where the context field is not yet assigned (e.g. fragment constructor before onAttach); Mockito/mock Context returning null from getApplicationContext(); refactoring static helpers that used to receive context and now get null.

Common situations: Fragment/view constructors invoked before attachment; unit tests passing null for speed; DI misconfiguration supplying a null provider.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/e9777ee7f095ce68. Report an issue: GitHub.