binarywang/WxJava · error · WxRuntimeException

参数个数必须为偶数

Error message

参数个数必须为偶数

What it means

Thrown by GsonHelper.put()/buildJsonObject() when the varargs keyOrValue array has an odd length. These helpers expect an alternating key,value,key,value... sequence; an odd count means a value is missing, so the method refuses to silently produce malformed JSON. It is a programmer/contract error, not data-driven.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java:176

   *
   * @param keyOrValue 包含key或value的数组
   * @return JsonObject对象.
   */
  public static JsonObject buildJsonObject(Object... keyOrValue) {
    JsonObject result = new JsonObject();
    put(result, keyOrValue);
    return result;
  }

  /**
   * 批量向JsonObject对象中添加属性
   *
   * @param jsonObject 原始JsonObject对象
   * @param keyOrValue 包含key或value的数组
   */
  public static void put(JsonObject jsonObject, Object... keyOrValue) {
    if (keyOrValue.length % 2 == 1) {
      throw new WxRuntimeException("参数个数必须为偶数");
    }

    for (int i = 0; i < keyOrValue.length / 2; i++) {
      final Object key = keyOrValue[2 * i];
      final Object value = keyOrValue[2 * i + 1];
      if (value == null) {
        jsonObject.add(key.toString(), null);
        continue;
      }

      if (value instanceof Boolean) {
        jsonObject.addProperty(key.toString(), (Boolean) value);
      } else if (value instanceof Character) {
        jsonObject.addProperty(key.toString(), (Character) value);
      } else if (value instanceof Number) {
        jsonObject.addProperty(key.toString(), (Number) value);
      } else if (value instanceof JsonElement) {
        jsonObject.add(key.toString(), (JsonElement) value);

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Recount the arguments to buildJsonObject/put and ensure they come in key/value pairs.
  2. If constructing the array dynamically, assert args.length % 2 == 0 before calling.
  3. Prefer the typed JsonObject.addProperty overloads for single fields to avoid varargs mistakes.

Example fix

// before
JsonObject o = GsonHelper.buildJsonObject("k1", v1, "k2");
// after
JsonObject o = GsonHelper.buildJsonObject("k1", v1, "k2", v2);
Defensive patterns

Strategy: validation

Validate before calling

Object[] kv = {"k1", v1, "k2", v2};
if (kv.length % 2 != 0) {
  throw new IllegalStateException("key/value pairs must be even");
}
JsonObject o = GsonHelper.buildJsonObject(kv);

Type guard

static boolean isEvenKv(Object... kv) { return kv != null && kv.length % 2 == 0; }

Try / catch

null

Prevention

When it happens

Trigger: Calling buildJsonObject("k1", v1, "k2") or put(obj, "k1", v1, "k2") with a dangling key, or passing a List/array flattened incorrectly so the element count is odd.

Common situations: Refactor that drops a value; copy-paste that adds a key without its value; dynamically building the varargs from a collection whose size assumption is wrong.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/e409d06ffb378d89. Report an issue: GitHub.