binarywang/WxJava · warning · IllegalArgumentException
表单字段值不能为null
Error message
表单字段值不能为null
What it means
CommonUploadParam.addFormField rejects a null field value. Unlike a blank name, the method treats null value as a programming error — to remove a field you omit the call, not pass null. This prevents silent malformed multipart bodies.
Source
Thrown at weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/CommonUploadParam.java:96
*/
@SneakyThrows
public static CommonUploadParam fromBytes(String name, @Nullable String fileName, byte[] bytes) {
return new CommonUploadParam(name, new CommonUploadData(fileName, new ByteArrayInputStream(bytes), bytes.length), null);
}
/**
* 添加额外的表单字段
*
* @param fieldName 表单字段名
* @param fieldValue 表单字段值
* @return 当前对象,支持链式调用
*/
public CommonUploadParam addFormField(String fieldName, String fieldValue) {
if (fieldName == null || fieldName.trim().isEmpty()) {
throw new IllegalArgumentException("表单字段名不能为空");
}
if (fieldValue == null) {
throw new IllegalArgumentException("表单字段值不能为null");
}
if (this.formFields == null) {
this.formFields = new HashMap<>();
}
this.formFields.put(fieldName, fieldValue);
return this;
}
@Override
public String toString() {
return String.format("{name:%s, data:%s, formFields:%s}", name, data, formFields);
}
}
View on GitHub (pinned to 1c43293a3c)
Solutions
- Coalesce null values to an empty string ("") if an empty part is acceptable, or skip the field if it is optional.
- Validate/require the value at the source so null never reaches addFormField.
- Log when a value is null so you can decide skip-vs-default behaviour intentionally.
Example fix
// before
param.addFormField("openid", userInfo.getOpenid()); // null if missing
// after
String openid = userInfo.getOpenid();
if (openid != null) {
param.addFormField("openid", openid);
} Defensive patterns
Strategy: validation
Validate before calling
if (fieldValue == null) {
// skip optional field, or default to empty string
} else {
param.addFormField(fieldName, fieldValue);
} Try / catch
try {
param.addFormField(name, value);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("表单字段值不能为null")) {
// default to "" or skip
}
throw e;
} Prevention
- Coalesce null values to "" only if an empty part is acceptable; otherwise skip the field.
- Validate upstream sources so null never reaches addFormField.
- Use Optional/Objects.requireNonNull at the data source to fail early.
When it happens
Trigger: Calling addFormField(name, null) where the value resolved to null (e.g. an unset map entry, a missing config value, or an uninitialised variable).
Common situations: Value looked up from a Map/properties that returned null; an upstream computation that can legitimately produce null but the caller forwarded it unconditionally.
Related errors
- 表单字段名不能为空
- 微信开放平台 appId 不能为空
- 微信开放平台 secret 不能为空
- uri参数中不允许有access_token: {uri}
- 不支持的http执行器类型:{requestType}
AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14).
Data as JSON: /api/errors/52a69ca1523f04d3.
Report an issue: GitHub.