didi/DoKit · error · IllegalArgumentException

Network policy cannot be null.

Error message

Network policy cannot be null.

What it means

Thrown by RequestCreator.networkPolicy(MemoryPolicy's counterpart) networkPolicy(NetworkPolicy, NetworkPolicy...) when the required first policy argument is null. NetworkPolicy flags (NO_CACHE, NO_STORE, OFFLINE) control OkHttp cache behavior; a null primary policy is rejected immediately with IllegalArgumentException.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/RequestCreator.java:369

        }
        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 policy
     * options using the varargs parameter.
     */
    public RequestCreator networkPolicy(NetworkPolicy policy, NetworkPolicy... additional) {
        if (policy == null) {
            throw new IllegalArgumentException("Network policy cannot be null.");
        }
        this.networkPolicy |= policy.index;
        if (additional == null) {
            throw new IllegalArgumentException("Network policy cannot be null.");
        }
        if (additional.length > 0) {
            for (NetworkPolicy networkPolicy : additional) {
                if (networkPolicy == null) {
                    throw new IllegalArgumentException("Network policy cannot be null.");
                }
                this.networkPolicy |= networkPolicy.index;
            }
        }
        return this;
    }

    /**
     * Disable brief fade in of images loaded from the disk cache or network.

View on GitHub (pinned to 626827cddb)

Solutions

  1. Pass a concrete NetworkPolicy, e.g. networkPolicy(OFFLINE) for offline mode
  2. Guard nullable values: if (policy != null) rc.networkPolicy(policy);
  3. Default the config field to a real policy so null never reaches the call

Example fix

// before
NetworkPolicy p = isOffline() ? NetworkPolicy.OFFLINE : null;
picasso.load(url).networkPolicy(p).into(imageView); // throws when null

// after
RequestCreator rc = picasso.load(url);
if (isOffline()) rc.networkPolicy(NetworkPolicy.OFFLINE);
rc.into(imageView);
Defensive patterns

Strategy: validation

Validate before calling

if (networkPolicy != null) {
    rc.networkPolicy(networkPolicy);
}

Type guard

static NetworkPolicy requireNetworkPolicy(NetworkPolicy p) {
    if (p == null) throw new IllegalArgumentException("NetworkPolicy required");
    return p;
}

Prevention

When it happens

Trigger: Calling networkPolicy(null), or passing a nullable variable (e.g. an offline-mode flag resolved to null) as the first argument.

Common situations: Offline/online mode switches where the policy is computed conditionally and the online branch yields null; config-driven network policies decoded from JSON/preferences; refactoring from deprecated APIs where null slipped through.

Related errors


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