xkcoding/spring-boot-demo · warning · RuntimeException

用户id不能为null

Error message

用户id不能为null

What it means

Thrown by updateUser when the User parameter is null. The guard uses ObjectUtil.isNull(user) (Hutool) and throws a plain RuntimeException. The message says 'user id cannot be null' but the actual check is for the entire user object being null — the message is slightly misleading. Additionally, even if user is non-null, user.getId() could still be null, which would cause a downstream failure in userDao.updateTemplateById.

Source

Thrown at demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java:73

     * 根据主键删除用户
     *
     * @param id 主键
     */
    @Override
    public void deleteUser(Long id) {
        userDao.deleteById(id);
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return 更新后的用户
     */
    @Override
    public User updateUser(User user) {
        if (ObjectUtil.isNull(user)) {
            throw new RuntimeException("用户id不能为null");
        }
        userDao.updateTemplateById(user);
        return userDao.single(user.getId());
    }

    /**
     * 查询单个用户
     *
     * @param id 主键id
     * @return 用户信息
     */
    @Override
    public User getUser(Long id) {
        return userDao.single(id);
    }

    /**
     * 查询用户列表

View on GitHub (pinned to 87a142f960)

Solutions

  1. Validate at the controller layer with @Valid and @NotNull on the User fields so null never reaches the service.
  2. Also check user.getId() != null before proceeding, since the message references id.
  3. Replace RuntimeException with a domain-specific exception (e.g., IllegalArgumentException) for clearer handling.
  4. Fix the message to accurately describe what was null (the user object, not just the id).

Example fix

// before
if (ObjectUtil.isNull(user)) {
    throw new RuntimeException("用户id不能为null");
}
userDao.updateTemplateById(user);
return userDao.single(user.getId());

// after — check both object and id, use a clearer exception
if (ObjectUtil.isNull(user) || ObjectUtil.isNull(user.getId())) {
    throw new IllegalArgumentException("用户或用户ID不能为null");
}
userDao.updateTemplateById(user);
return userDao.single(user.getId());
Defensive patterns

Strategy: validation

Validate before calling

// Validate both the object and its id before calling updateUser
if (user == null || user.getId() == null) {
    throw new IllegalArgumentException("User and user ID must not be null");
}

Try / catch

try {
    userService.updateUser(user);
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("不能为null")) {
        // Handle the null-argument case
        return ResponseEntity.badRequest().body(e.getMessage());
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling updateUser(null) from a controller or service layer. The method does not validate that user.getId() is non-null before passing it to userDao.single(user.getId()) on the return path, so a non-null user with a null id will also fail — but with a different (BeetlSQL) error.

Common situations: Controller receives an empty request body that deserializes to null; a caller passes null by mistake; JSON binding fails silently producing null; the id field is not set on a partially-populated object.

Related errors


AI-assisted analysis of xkcoding/spring-boot-demo@87a142f960 (2026-08-14). Data as JSON: /api/errors/751d3aab3465fc04. Report an issue: GitHub.