{"record":{"id":"e9777ee7f095ce68","repo":"didi/DoKit","slug":"context-must-not-be-null","errorCode":null,"errorMessage":"Context must not be null.","messagePattern":"Context must not be null\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/DokitPicasso.java","lineNumber":702,"sourceCode":"  /** Fluent API for creating {@link DokitPicasso} instances. */\n  @SuppressWarnings(\"UnusedDeclaration\") // Public API.\n  public static class Builder {\n    private final Context context;\n    private Downloader downloader;\n    private ExecutorService service;\n    private Cache cache;\n    private Listener listener;\n    private RequestTransformer transformer;\n    private List<RequestHandler> requestHandlers;\n    private Bitmap.Config defaultBitmapConfig;\n\n    private boolean indicatorsEnabled;\n    private boolean loggingEnabled;\n\n    /** Start building a new {@link DokitPicasso} instance. */\n    public Builder(Context context) {\n      if (context == null) {\n        throw new IllegalArgumentException(\"Context must not be null.\");\n      }\n      this.context = context.getApplicationContext();\n    }\n\n    /**\n     * Specify the default {@link Bitmap.Config} used when decoding images. This can be overridden\n     * on a per-request basis using {@link RequestCreator#config(Bitmap.Config) config(..)}.\n     */\n    public Builder defaultBitmapConfig(Bitmap.Config bitmapConfig) {\n      if (bitmapConfig == null) {\n        throw new IllegalArgumentException(\"Bitmap config must not be null.\");\n      }\n      this.defaultBitmapConfig = bitmapConfig;\n      return this;\n    }\n\n    /** Specify the {@link Downloader} that will be used for downloading images. */\n    public Builder downloader(Downloader downloader) {","sourceCodeStart":684,"sourceCodeEnd":720,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/DokitPicasso.java#L684-L720","documentation":"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.","triggerScenarios":"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.","commonSituations":"Fragment/view constructors invoked before attachment; unit tests passing null for speed; DI misconfiguration supplying a null provider.","solutions":["Defer building until the context is real: fragment onAttach / Activity onCreate","Use context.getApplicationContext() explicitly when wiring from a short-lived object","In DI (Hilt/Dagger), make the Context provider @NonNull and fail at graph validation"],"exampleFix":"// before\npublic AvatarLoader(Context ctx) {\n  this.picasso = new DokitPicasso.Builder(ctx).build(); // ctx null in ctor\n}\n\n// after\npublic AvatarLoader(Context ctx) {\n  this.picasso = new DokitPicasso.Builder(\n      Objects.requireNonNull(ctx, \"context\").getApplicationContext()).build();\n}","handlingStrategy":"validation","validationCode":"if (context != null) {\n  DokitPicasso picasso = new DokitPicasso.Builder(context.getApplicationContext()).build();\n}","typeGuard":"java.util.Objects.requireNonNull;","tryCatchPattern":"try {\n  picasso = new DokitPicasso.Builder(context).build();\n} catch (IllegalArgumentException e) {\n  if (\"Context must not be null.\".equals(e.getMessage())) {\n    throw new IllegalStateException(\"Picasso built before context attached\", e);\n  }\n  throw e;\n}","preventionTips":["Build Picasso only where a lifecycle-guaranteed Context exists (Activity.onCreate, Fragment.onAttach)","Pass getApplicationContext() to avoid leaking the wrapper","Kotlin: make the context parameter non-nullable in your own wrappers"],"tags":["android","picasso","context","null-safety","initialization"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}