{"record":{"id":"9fad1d993350216e","repo":"binarywang/WxJava","slug":"setattribute-name-parameter-cannot-be-null","errorCode":null,"errorMessage":"setAttribute: name parameter cannot be null","messagePattern":"setAttribute: name parameter cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java","lineNumber":107,"sourceCode":"    return this.attributes.get(name);\n  }\n\n  @Override\n  public Enumeration<String> getAttributeNames() {\n    if (!isValidInternal()) {\n      throw new IllegalStateException(SM.getString(\"sessionImpl.getAttributeNames.ise\"));\n    }\n\n    Set<String> names = new HashSet<>();\n    names.addAll(this.attributes.keySet());\n    return Collections.enumeration(names);\n  }\n\n  @Override\n  public void setAttribute(String name, Object value) {\n    // Name cannot be null\n    if (name == null) {\n      throw new IllegalArgumentException(SM.getString(\"sessionImpl.setAttribute.namenull\"));\n    }\n\n    // Null value is the same as removeAttribute()\n    if (value == null) {\n      removeAttribute(name);\n      return;\n    }\n\n    // Validate our current state\n    if (!isValidInternal()) {\n      throw new IllegalStateException(SM.getString(\"sessionImpl.setAttribute.ise\", getIdInternal()));\n    }\n\n    this.attributes.put(name, value);\n\n  }\n\n  @Override","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/binarywang/WxJava/blob/1c43293a3c2c9d7e91304b6d037fb017f680d0c6/weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java#L89-L125","documentation":"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.","triggerScenarios":"Calling session.setAttribute(null, value) where the key was computed/dereferenced and resolved to null.","commonSituations":"Attribute name read from a config/properties source that returned null; dynamically generated keys that produced null; refactoring left a null key path.","solutions":["Ensure the attribute name is a non-null constant or validated value.","Guard with a null check and skip or log if the key is absent.","Use an enum or constant for attribute names to prevent null at compile time."],"exampleFix":"// before\nsession.setAttribute(userField, value); // userField null if config missing\n\n// after\nif (userField != null) {\n  session.setAttribute(userField, value);\n}","handlingStrategy":"validation","validationCode":"if (name != null) {\n  session.setAttribute(name, value);\n}","typeGuard":null,"tryCatchPattern":"try {\n  session.setAttribute(name, value);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"name parameter cannot be null\")) {\n    // fix the null name source\n  }\n  throw e;\n}","preventionTips":["Use constants/enums for attribute names so null keys are impossible.","Guard dynamic names with a null check before setAttribute.","Validate name sources (config/maps) at load time."],"tags":["session","null-key","validation","precondition"],"backgroundTag":null,"analyzedSha":"1c43293a3c2c9d7e91304b6d037fb017f680d0c6","analyzedAt":"2026-08-14T02:29:11.060Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}