binarywang/WxJava · error · IllegalArgumentException

缺少agentid参数

Error message

缺少agentid参数

What it means

Thrown by WxCpAgentServiceImpl.get(Long agentId) when agentId is null. The agentId is required by the AGENT_GET URL template (String.format with %s), so a null would both break the URL and be semantically invalid; the method rejects it with IllegalArgumentException up front.

Source

Thrown at weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java:39

/**
 * <pre>
 *  管理企业号应用
 *  Created by huansinho on 2018/4/13.
 * </pre>
 *
 * @author <a href="https://github.com/huansinho">huansinho</a>
 */
@RequiredArgsConstructor
public class WxCpAgentServiceImpl implements WxCpAgentService {


  private final WxCpService mainService;

  @Override
  public WxCpAgent get(Long agentId) throws WxErrorException {
    if (agentId == null) {
      throw new IllegalArgumentException("缺少agentid参数");
    }

    final String url = String.format(this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_GET), agentId);
    return WxCpAgent.fromJson(this.mainService.get(url, null));
  }

  @Override
  public void set(WxCpAgent agentInfo) throws WxErrorException {
    String url = this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_SET);
    String responseContent = this.mainService.post(url, agentInfo.toJson());
    JsonObject jsonObject = GsonParser.parse(responseContent);
    if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() != 0) {
      throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
    }
  }

  @Override
  public List<WxCpAgent> list() throws WxErrorException {

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Provide a non-null agentId sourced from configuration.
  2. Null-check before calling and fail with a clear domain error if it is missing.
  3. Validate required config at startup so agentId cannot be null at runtime.

Example fix

// before
WxCpAgent a = agentService.get(agentId); // agentId may be null
// after
if (agentId == null) throw new IllegalArgumentException("agentId not configured");
WxCpAgent a = agentService.get(agentId);
Defensive patterns

Strategy: validation

Validate before calling

if (agentId == null) throw new IllegalArgumentException("agentId not configured");
agentService.get(agentId);

Type guard

static boolean validAgentId(Long id) { return id != null; }

Try / catch

null

Prevention

When it happens

Trigger: Calling agentService.get(null) because the agentId was not configured, came from a missing property, or was a null Long from a map/optional.

Common situations: Multi-agent app where the agentId is optional/variable and was unset; reading agentId from config that was not provided; passing a Long that was never assigned.

Related errors


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