didi/DoKit · error · NullPointerException
headers == null
Error message
headers == null
What it means
Thrown by the Map overload CommonHeaders.of(Map<String, String> headers) when the passed map is null. This factory flattens the map into the internal name/value array, so a non-null map is a hard precondition.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/common/CommonHeaders.java:189
}
// Check for malformed headers.
for (int i = 0; i < namesAndValues.length; i += 2) {
String name = namesAndValues[i];
String value = namesAndValues[i + 1];
if (name.length() == 0 || name.indexOf('\0') != -1 || value.indexOf('\0') != -1) {
throw new IllegalArgumentException("Unexpected header: " + name + ": " + value);
}
}
return new CommonHeaders(namesAndValues);
}
/**
* Returns headers for the header names and values in the {@link Map}.
*/
public static CommonHeaders of(Map<String, String> headers) {
if (headers == null) throw new NullPointerException("headers == null");
// Make a defensive copy and clean it up.
String[] namesAndValues = new String[headers.size() * 2];
int i = 0;
for (Map.Entry<String, String> header : headers.entrySet()) {
if (header.getKey() == null || header.getValue() == null) {
throw new IllegalArgumentException("Headers cannot be null");
}
String name = header.getKey().trim();
String value = header.getValue().trim();
if (name.length() == 0 || name.indexOf('\0') != -1 || value.indexOf('\0') != -1) {
throw new IllegalArgumentException("Unexpected header: " + name + ": " + value);
}
namesAndValues[i] = name;
namesAndValues[i + 1] = value;
i += 2;
}
View on GitHub (pinned to 626827cddb)
Solutions
- Default the map: of(map == null ? Collections.emptyMap() : map)
- Use CommonHeaders.EMPTY-style empty Builder result when no headers are needed
- Fix the config layer to always supply a non-null (possibly empty) map
Example fix
// before CommonHeaders h = CommonHeaders.of(extraHeaders); // extraHeaders == null by default // after CommonHeaders h = CommonHeaders.of(extraHeaders != null ? extraHeaders : new HashMap<String, String>());
Defensive patterns
Strategy: type-guard
Validate before calling
Map<String, String> safe = (headersMap == null) ? new LinkedHashMap<String, String>() : headersMap; CommonHeaders h = CommonHeaders.of(safe);
Type guard
boolean isNonNullHeaderMap(Map<String, String> m) { return m != null; } Prevention
- Default optional header maps to empty maps at the config layer
- Treat 'no extra headers' as emptyMap(), never null
When it happens
Trigger: Calling CommonHeaders.of((Map<String, String>) null), e.g. forwarding request.headers plus a nullable extra-headers map that was never defaulted.
Common situations: Optional request extras (extra headers map from a mock template or config) that are null by default and are forwarded without a null check.
Related errors
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/9fa883b4d4df62fa.
Report an issue: GitHub.