Search code examples
springspring-bootspring-data-restspring-restcontroller

Random NullPointerExceptions with missing stacktrace in DispatcherServlet


I see random NullPointerException when one of my endpoints is accessed.

I know what an NPE is, but please bear with me, I have many issues here :

  • problem happens maybe once in every 10000 calls
  • if I replay the same request a second time it works OK
  • it looks as if it doesn't get to the code in my controller (no statements are logged)
  • and, most annoying... I don't have a stacktrace to help me pinpoint the problem.

This is what it is logged :

[p-nio-80-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

Annnnnd, that's it, nothing more...

The code of the controller is rather straithforward, it looks like this :

@RepositoryRestController
@ResponseBody
public class AnimalImportEndpoint {

    @Inject
    private animalImportService;

    @RequestMapping(value = "/animals/import", method = RequestMethod.POST)
    public AnimalImportResult import(@Valid @RequestBody Animal animal) {
        return animalImportService.import(animal);
    }

    @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Illegal arguments")
    @ExceptionHandler(IllegalArgumentException.class)
    public void illegalArguments() {
    }
}

The class is annoted with @RepositoryRestController instead of @RestController because Spring Data Rest also exposes /animals.

Any pointers or advice on how to troubleshoot this ?

Edit : other interesting fact, I use sleuth to trace requests. When I get this NullPointerException, I can see that there is no spanId/traceId in my logs ...


Solution

  • Add this JVM argument to disable stacktrace removal optimization

    -XX:-OmitStackTraceInFastThrow