yudaocode/SpringBoot-Labs · error · NullPointerException

没有粗面鱼丸

Error message

没有粗面鱼丸

What it means

The WebFlux counterpart of the MVC NPE demo: GET /user/exception-01 in a Spring WebFlux (reactive) application throws NullPointerException('没有粗面鱼丸') directly from the controller method. Because the method returns a plain UserVO (not a reactive type), WebFlux still funnels the thrown exception into the error channel, where an @ExceptionHandler in a @ControllerAdvice (or the default WebExceptionHandler chain, e.g. DefaultErrorWebExceptionHandler) renders it — by default as a JSON {timestamp, path, status:500, error, message} body.

Source

Thrown at lab-27/lab-27-webflux-02/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java:101

     * 获得指定用户编号的用户
     *
     * @param id 用户编号
     * @return 用户
     */
    @GetMapping("/get4")
    public CommonResult<UserVO> get4(@RequestParam("id") Integer id) {
        // 查询用户
        UserVO user = new UserVO().setId(id).setUsername("username:" + id);
        // 返回
        return CommonResult.success(user);
    }

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

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

//    @PostMapping(value = "/add",
//            // ↓ 增加 "application/xml"、"application/json" ,针对 Content-Type 请求头
//            consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
//            // ↓ 增加 "application/xml"、"application/json" ,针对 Accept 请求头
//            produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}
//    )

    @PostMapping(value = "/add",

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. Treat it as demo code: call it only to confirm your error WebExceptionHandler/@ControllerAdvice output shape.
  2. Register an @ExceptionHandler(NullPointerException.class) (or Exception-level) in a @ControllerAdvice so clients get your unified CommonResult JSON instead of the default error attributes.
  3. If this NPE appears unintentionally in WebFlux, remember the stack trace may point to reactor internals — enable checkpoint() or onOperatorDebug to locate the lambda that threw.
  4. Avoid throwing from controller bodies in real reactive code; return Mono.error(...) so the error travels the reactive chain predictably.

Example fix

// before (throwing imperatively in WebFlux)
@GetMapping("/exception-01")
public UserVO exception01() {
    throw new NullPointerException("没有粗面鱼丸");
}

// after (route the error through the reactive chain + unified handler)
@GetMapping("/exception-01")
public Mono<UserVO> exception01() {
    return Mono.error(new NullPointerException("没有粗面鱼丸"));
}

@RestControllerAdvice
class GlobalExceptionHandler {
    @ExceptionHandler(NullPointerException.class)
    CommonResult<?> npe(NullPointerException ex) { return CommonResult.error(...); }
}
Defensive patterns

Strategy: try-catch

Try / catch

// WebFlux: handle in a @ControllerAdvice, or inside the chain with onErrorResume:
@GetMapping("/exception-01")
public Mono<UserVO> exception01() {
    return Mono.error(new NullPointerException("没有粗面鱼丸"))
               .onErrorResume(ex -> Mono.empty()); // or map to CommonResult

Prevention

When it happens

Trigger: Calling GET /user/exception-01 on the WebFlux server (default port 8080 unless configured). The throw is unconditional.

Common situations: Used to verify exception handling in reactive stacks. Real-world analogue: a blocking/unintentional NPE inside a WebFlux controller; if it happens inside a reactive operator (map/flatMap lambda) instead of the controller body, the same error surfaces via onError propagation, and the stack trace shows operator assembly points, which confuses developers migrating from MVC.

Related errors


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