binarywang/WxJava · warning · IllegalArgumentException

setAttribute: name parameter cannot be null

Error message

setAttribute: name parameter cannot be null

What it means

StandardSession.setAttribute throws IllegalArgumentException when the name argument is null. A session attribute needs a key; a null key has no meaning and is rejected before any validity/state check. Note this check runs BEFORE the validity check, so even a valid session rejects a null name.

Source

Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java:107

    return this.attributes.get(name);
  }

  @Override
  public Enumeration<String> getAttributeNames() {
    if (!isValidInternal()) {
      throw new IllegalStateException(SM.getString("sessionImpl.getAttributeNames.ise"));
    }

    Set<String> names = new HashSet<>();
    names.addAll(this.attributes.keySet());
    return Collections.enumeration(names);
  }

  @Override
  public void setAttribute(String name, Object value) {
    // Name cannot be null
    if (name == null) {
      throw new IllegalArgumentException(SM.getString("sessionImpl.setAttribute.namenull"));
    }

    // Null value is the same as removeAttribute()
    if (value == null) {
      removeAttribute(name);
      return;
    }

    // Validate our current state
    if (!isValidInternal()) {
      throw new IllegalStateException(SM.getString("sessionImpl.setAttribute.ise", getIdInternal()));
    }

    this.attributes.put(name, value);

  }

  @Override

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Ensure the attribute name is a non-null constant or validated value.
  2. Guard with a null check and skip or log if the key is absent.
  3. Use an enum or constant for attribute names to prevent null at compile time.

Example fix

// before
session.setAttribute(userField, value); // userField null if config missing

// after
if (userField != null) {
  session.setAttribute(userField, value);
}
Defensive patterns

Strategy: validation

Validate before calling

if (name != null) {
  session.setAttribute(name, value);
}

Try / catch

try {
  session.setAttribute(name, value);
} catch (IllegalArgumentException e) {
  if (e.getMessage().contains("name parameter cannot be null")) {
    // fix the null name source
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling session.setAttribute(null, value) where the key was computed/dereferenced and resolved to null.

Common situations: Attribute name read from a config/properties source that returned null; dynamically generated keys that produced null; refactoring left a null key path.

Related errors


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