didi/DoKit · error · IllegalArgumentException
Memory policy cannot be null.
Error message
Memory policy cannot be null.
What it means
Thrown by RequestCreator.memoryPolicy(MemoryPolicy, MemoryPolicy...) when the required first policy argument is null. MemoryPolicy is an enum-like set of flags (NO_CACHE, NO_STORE) and a null primary policy indicates a programming error, rejected immediately with IllegalArgumentException.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/RequestCreator.java:346
data.transform(transformations);
return this;
}
/**
* @deprecated Use {@link #memoryPolicy(MemoryPolicy, MemoryPolicy...)} instead.
*/
@Deprecated
public RequestCreator skipMemoryCache() {
return memoryPolicy(NO_CACHE, NO_STORE);
}
/**
* Specifies the {@link MemoryPolicy} to use for this request. You may specify additional policy
* options using the varargs parameter.
*/
public RequestCreator memoryPolicy(MemoryPolicy policy, MemoryPolicy... additional) {
if (policy == null) {
throw new IllegalArgumentException("Memory policy cannot be null.");
}
this.memoryPolicy |= policy.index;
if (additional == null) {
throw new IllegalArgumentException("Memory policy cannot be null.");
}
if (additional.length > 0) {
for (MemoryPolicy memoryPolicy : additional) {
if (memoryPolicy == null) {
throw new IllegalArgumentException("Memory policy cannot be null.");
}
this.memoryPolicy |= memoryPolicy.index;
}
}
return this;
}
/**
* Specifies the {@link NetworkPolicy} to use for this request. You may specify additional policyView on GitHub (pinned to 626827cddb)
Solutions
- Pass a non-null MemoryPolicy, e.g. memoryPolicy(NO_CACHE, NO_STORE) instead of the deprecated skipMemoryCache()
- Guard nullable config values: memoryPolicy(policy != null ? policy : MemoryPolicy.NO_CACHE)
- Default the config field to a concrete policy so it can never be null
Example fix
// before MemoryPolicy p = config.isCacheDisabled() ? MemoryPolicy.NO_CACHE : null; picasso.load(url).memoryPolicy(p).into(imageView); // throws when null // after RequestCreator rc = picasso.load(url); if (config.isCacheDisabled()) rc.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE); rc.into(imageView);
Defensive patterns
Strategy: validation
Validate before calling
MemoryPolicy p = (configPolicy != null) ? configPolicy : MemoryPolicy.NO_CACHE; rc.memoryPolicy(p);
Type guard
static MemoryPolicy requireMemoryPolicy(MemoryPolicy p) {
if (p == null) throw new IllegalArgumentException("MemoryPolicy required");
return p;
} Prevention
- Default nullable policy config to a concrete MemoryPolicy at load time
- Prefer memoryPolicy(NO_CACHE, NO_STORE) over the deprecated skipMemoryCache()
- Note the first policy is applied before the varargs checks, so a throw can leave partial state — avoid reusing that builder
When it happens
Trigger: Calling memoryPolicy(null), or passing a nullable variable (e.g. a policy read from config/preferences) as the first argument: .memoryPolicy(maybeNullPolicy).
Common situations: Configuration-driven cache policies where the flag was absent from JSON/preferences and decoded to null; Kotlin call sites passing MemoryPolicy? without a null check; refactors away from the deprecated skipMemoryCache().
Related errors
- Error image may not be null.
- Tag invalid.
- Network policy cannot be null.
- Target must not be null.
- RemoteViews must not be null.
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/e8f7bda7a152aa99.
Report an issue: GitHub.