{"record":{"id":"751d3aab3465fc04","repo":"xkcoding/spring-boot-demo","slug":"id-null","errorCode":null,"errorMessage":"用户id不能为null","messagePattern":"用户id不能为null","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"warning","filePath":"demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java","lineNumber":73,"sourceCode":"     * 根据主键删除用户\n     *\n     * @param id 主键\n     */\n    @Override\n    public void deleteUser(Long id) {\n        userDao.deleteById(id);\n    }\n\n    /**\n     * 更新用户\n     *\n     * @param user 用户\n     * @return 更新后的用户\n     */\n    @Override\n    public User updateUser(User user) {\n        if (ObjectUtil.isNull(user)) {\n            throw new RuntimeException(\"用户id不能为null\");\n        }\n        userDao.updateTemplateById(user);\n        return userDao.single(user.getId());\n    }\n\n    /**\n     * 查询单个用户\n     *\n     * @param id 主键id\n     * @return 用户信息\n     */\n    @Override\n    public User getUser(Long id) {\n        return userDao.single(id);\n    }\n\n    /**\n     * 查询用户列表","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/xkcoding/spring-boot-demo/blob/87a142f9604c1a5365b4d24d22c2c11c26a9d5ab/demo-orm-beetlsql/src/main/java/com/xkcoding/orm/beetlsql/service/impl/UserServiceImpl.java#L55-L91","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate at the controller layer with @Valid and @NotNull on the User fields so null never reaches the service.","Also check user.getId() != null before proceeding, since the message references id.","Replace RuntimeException with a domain-specific exception (e.g., IllegalArgumentException) for clearer handling.","Fix the message to accurately describe what was null (the user object, not just the id)."],"exampleFix":"// before\nif (ObjectUtil.isNull(user)) {\n    throw new RuntimeException(\"用户id不能为null\");\n}\nuserDao.updateTemplateById(user);\nreturn userDao.single(user.getId());\n\n// after — check both object and id, use a clearer exception\nif (ObjectUtil.isNull(user) || ObjectUtil.isNull(user.getId())) {\n    throw new IllegalArgumentException(\"用户或用户ID不能为null\");\n}\nuserDao.updateTemplateById(user);\nreturn userDao.single(user.getId());","handlingStrategy":"validation","validationCode":"// Validate both the object and its id before calling updateUser\nif (user == null || user.getId() == null) {\n    throw new IllegalArgumentException(\"User and user ID must not be null\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    userService.updateUser(user);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"不能为null\")) {\n        // Handle the null-argument case\n        return ResponseEntity.badRequest().body(e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Use @Valid and @NotNull on the User class fields at the controller boundary.","Check both user and user.getId() for null in the service layer.","Use a domain-specific exception (IllegalArgumentException) instead of RuntimeException for clearer handling.","Fix the message to accurately state what is null."],"tags":["beetlsql","validation","null-check","orm","argument-validation"],"backgroundTag":null,"analyzedSha":"87a142f9604c1a5365b4d24d22c2c11c26a9d5ab","analyzedAt":"2026-08-14T01:16:58.217Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}