hs-web/hsweb-framework · warning · NullPointerException

e.getMessage()

Error message

e.getMessage()

What it means

In the servlet (WebMvc) variant of the error advice, IllegalArgumentException is mapped to HTTP 400 with code illegal_argument and a message resolved via resolveMessage(e) — which tries to localize e.getMessage(). The 'e.getMessage()' placeholder indicates the raw IllegalArgumentException text is what a client normally sees when no localized key matches. This fires for argument-validation failures inside MVC controllers/services.

Solutions

  1. Fix the caller to supply the required argument/value described in the exception message.
  2. Validate inputs at the edge with @Valid/@NotNull or explicit checks so users get field-level 400 errors.
  3. Use a localized message key as the IllegalArgumentException text so resolveMessage(e) yields a translatable message.
  4. Prefer dedicated exceptions (NotFoundException, ForbiddenException) for non-argument failures to get correct HTTP codes.

Example fix

// before
throw new IllegalArgumentException("id is null");
// after
throw new IllegalArgumentException("error.id_not_null"); // resolvable via i18n bundle
Defensive patterns

Strategy: validation

Validate before calling

// Java caller-side check mirroring server expectations
if (id == null || id.isEmpty()) {
    throw new IllegalArgumentException("error.id_required");
}

Try / catch

try {
    return controller.handle(param);
} catch (IllegalArgumentException e) {
    return ResponseMessage.error(400, "illegal_argument",
        LocaleUtils.resolveMessage(e.getMessage(), e.getMessage()));
}

Prevention

When it happens

Trigger: A controller/service calls e.g. Assert/Assert.notNull, StringUtils checks, or throws new IllegalArgumentException("...") for bad request parameters in a Spring MVC (non-WebFlux) deployment, reaching the handleException(IllegalArgumentException) at line 180.

Common situations: Missing or malformed query/body parameters; hsweb dynamic-query term strings with bad syntax (like the DimensionTerm error); programmatic validation in service layer; enum parsing of user input.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of hs-web/hsweb-framework@b2cfc85a57 (2026-09-13). Data as JSON: /api/errors/7de04b49413b4c6b. Report an issue: GitHub.

Appendix: source

Thrown at hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/CommonWebMvcErrorControllerAdvice.java:180

    @Order
    public ResponseMessage<Object> handleException(HttpMessageNotReadableException e) {
        return ResponseMessage
            .error(400,
                   "missing_request_body",
                   LocaleUtils.resolveMessage("error.missing_request_body"));
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseMessage<Object> handleException(NullPointerException e) {
        log.warn(e.getLocalizedMessage(), e);
        return ResponseMessage.error(e.getMessage());
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseMessage<Object> handleException(IllegalArgumentException e) {
        log.warn(e.getLocalizedMessage(), e);

        return ResponseMessage.error(400, CodeConstants.Error.illegal_argument, resolveMessage(e));
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseMessage<Object> handleException(AuthenticationException e) {
        log.warn(e.getLocalizedMessage(), e);

        return ResponseMessage.error(400, e.getCode(), resolveMessage(e));
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public ResponseMessage<Object> handleException(UnsupportedMediaTypeStatusException e) {
        log.warn(e.getLocalizedMessage(), e);

        return ResponseMessage

View on GitHub (pinned to b2cfc85a57)