didi/DoKit · error · IllegalArgumentException

Expected alternating header names and values

Error message

Expected alternating header names and values

What it means

Thrown by CommonHeaders.of(String... namesAndValues) when the number of elements is odd. Headers are stored as a flat alternating array [name0, value0, name1, value1, ...]; an odd count means a name is missing its value, which would corrupt every subsequent pair during reads.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/network/common/CommonHeaders.java:163

  }

  private static String get(String[] namesAndValues, String name) {
    for (int i = namesAndValues.length - 2; i >= 0; i -= 2) {
      if (name.equalsIgnoreCase(namesAndValues[i])) {
        return namesAndValues[i + 1];
      }
    }
    return null;
  }

  /**
   * Returns headers for the alternating header names and values. There must be an even number of
   * arguments, and they must alternate between header names and values.
   */
  public static CommonHeaders of(String... namesAndValues) {
    if (namesAndValues == null) throw new NullPointerException("namesAndValues == null");
    if (namesAndValues.length % 2 != 0) {
      throw new IllegalArgumentException("Expected alternating header names and values");
    }

    // Make a defensive copy and clean it up.
    namesAndValues = namesAndValues.clone();
    for (int i = 0; i < namesAndValues.length; i++) {
      if (namesAndValues[i] == null) throw new IllegalArgumentException("Headers cannot be null");
      namesAndValues[i] = namesAndValues[i].trim();
    }

    // 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);
      }
    }

View on GitHub (pinned to 626827cddb)

Solutions

  1. Ensure headers are always added as name+value pairs — check that args.length % 2 == 0 before the call
  2. Fix the construction site: every add of a header name must be paired with its value in the same statement
  3. Switch to CommonHeaders.Builder and call add(name, value) per header so pairs are structurally guaranteed

Example fix

// before
CommonHeaders.of("Accept", "*/*", "X-Trace-Id"); // odd count

// after
CommonHeaders.of("Accept", "*/*", "X-Trace-Id", traceId);
Defensive patterns

Strategy: validation

Validate before calling

if (headerArray != null && headerArray.length % 2 == 0) { CommonHeaders.of(headerArray); } else { /* fix pairs upstream */ }

Prevention

When it happens

Trigger: Calling CommonHeaders.of("Content-Type", "application/json", "Authorization") — a trailing header name with no value; commonly happens when dynamically appending a single header to an array without extending it by two slots.

Common situations: Building header arrays in loops that append a name and conditionally skip the value; copy-paste mistakes when adding an extra header to a literal array.

Related errors


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