yudaocode/SpringBoot-Labs · error · NullPointerException

没有粗面鱼丸

Error message

没有粗面鱼丸

What it means

This is an intentionally thrown NullPointerException from the demo endpoint GET /user/exception-01 in a Spring MVC lab project. It is not a real defect: the lab uses it to demonstrate how an uncaught runtime exception propagates through DispatcherServlet and how a @ControllerAdvice global handler can convert it into a unified error payload (CommonResult). Unless such a handler exists, Spring Boot maps it to HTTP 500 with the message '没有粗面鱼丸' (a joke string meaning 'no macaroni fish balls').

Source

Thrown at lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java:73

     * 获得指定用户编号的用户
     *
     * 测试个问题
     *
     * @param id 用户编号
     * @return 用户
     */
    @PostMapping("/get")
    public UserVO get3(@RequestParam("id") Integer id) {
        // 查询并返回用户
        return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
    }

    /**
     * 测试抛出 NullPointerException 异常
     */
    @GetMapping("/exception-01")
    public UserVO exception01() {
        throw new NullPointerException("没有粗面鱼丸");
    }

    /**
     * 测试抛出 ServiceException 异常
     */
    @GetMapping("/exception-02")
    public UserVO exception02() {
        throw new ServiceException(ServiceExceptionEnum.USER_NOT_FOUND);
    }

    @GetMapping("/do_something")
    public void doSomething() {
        logger.info("[doSomething]");
    }

    @GetMapping("/current_user")
    public UserVO currentUser() {
        logger.info("[currentUser]");

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. Recognize this is deliberate demo code: hit the endpoint only when testing the global exception handler, never from real client code.
  2. Confirm a @RestControllerAdvice with @ExceptionHandler(NullPointerException.class) (or a catch-all Exception handler) exists in the same app context so the response is a structured CommonResult instead of a raw 500.
  3. If this appears in logs unexpectedly during unrelated testing, find who is calling /user/exception-01 (access log, browser tab left open) rather than debugging the stack trace itself.
  4. For real code, eliminate NPE sources with Optional, null checks, or Objects.requireNonNull on inputs.

Example fix

// before (demo, throws raw NPE)
@GetMapping("/exception-01")
public UserVO exception01() {
    throw new NullPointerException("没有粗面鱼丸");
}

// after (convert to unified error via global handler)
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(NullPointerException.class)
    public CommonResult<?> handleNpe(NullPointerException ex) {
        return CommonResult.error(ServiceExceptionEnum.INTERNAL_SERVER_ERROR);
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// No pre-call validation possible: endpoint throws unconditionally.
// Guard the demo from real traffic instead:
// application.yaml: only map demo servlet in dev
# spring.profiles.active: dev

Try / catch

// In an @RestControllerAdvice (preferred over per-caller catch):
@ExceptionHandler(NullPointerException.class)
public CommonResult<?> handleNpe(NullPointerException ex) {
    logger.warn("NPE in handler", ex);
    return CommonResult.error(ServiceExceptionEnum.INTERNAL_SERVER_ERROR);
}

Prevention

When it happens

Trigger: Calling GET /user/exception-01 (the @GetMapping("/exception-01") mapping on UserController). No parameters needed; the method body unconditionally executes throw new NullPointerException("没有粗面鱼丸").

Common situations: Only occurs when exercising this tutorial endpoint deliberately (e.g., curl http://localhost:8080/user/exception-01). Real-world analogues: any NPE raised inside a @RequestMapping method that lacks an @ExceptionHandler(NPE.class)/global exception handler, surfacing as a whitelabel 500 error.

Related errors


AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14). Data as JSON: /api/errors/64a898ed469cf2ec. Report an issue: GitHub.