binarywang/WxJava · error · IllegalArgumentException

key may not have a null value

Error message

key may not have a null value

What it means

Thrown by StringManager.getString(String) when key is null. StringManager is a resource-bundle wrapper; passing a null key is treated as a programming error and rejected with IllegalArgumentException before the bundle is consulted.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java:188

    }
    // Return the default
    return getManager(packageName);
  }

  /**
   * Get a string from the underlying resource bundle or return
   * null if the String is not found.
   *
   * @param key to desired resource String
   * @return resource String matching <i>key</i> from underlying
   * bundle or null if not found.
   * @throws IllegalArgumentException if <i>key</i> is null.
   */
  public String getString(String key) {
    if (key == null) {
      String msg = "key may not have a null value";

      throw new IllegalArgumentException(msg);
    }

    String str = null;

    try {
      // Avoid NPE if bundle is null and treat it like an MRE
      if (this.bundle != null) {
        str = this.bundle.getString(key);
      }
    } catch (MissingResourceException mre) {
      //bad: shouldn't mask an exception the following way:
      //   str = "[cannot find message associated with key '" + key +
      //         "' due to " + mre + "]";
      //     because it hides the fact that the String was missing
      //     from the calling code.
      //good: could just throw the exception (or wrap it in another)
      //      but that would probably cause much havoc on existing
      //      code.

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Null-check the key before calling getString, or default it to a known key.
  2. Audit callers that build keys dynamically and ensure they never pass null.
  3. Log the caller/source of the null key to find the upstream missing value.

Example fix

// before
String msg = sm.getString(key); // key may be null
// after
String msg = (key == null) ? sm.getString("error.default") : sm.getString(key);
Defensive patterns

Strategy: validation

Validate before calling

if (key == null) throw new IllegalArgumentException("message key required");
String msg = sm.getString(key);

Type guard

static boolean nonNullKey(String k) { return k != null; }

Try / catch

null

Prevention

When it happens

Trigger: Calling getString(null) directly, or passing a variable that resolved to null (a missing config key, a map lookup returning null, an uninitialised field).

Common situations: Building an error message key from a code that has no mapping; lookup from a properties source where the key was absent and null propagated; refactor leaving a null key path.

Related errors


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