ExceptionHandler for NullPointerException

I’m trying to setup an ExceptionHandler for the NullpointerException, following the [documentation]

I have already implemented ExceptionHandlers for other Exceptions (my own exceptions) and they work great, but I’m having trouble with handling NPEs.

Basically, when a NPE occurs, I see the stacktrace in my console, but in the browser nothing happens. Is this by design? Is maybe the NPE an exception in the exception handling logic of vaadin?

Funny is though, that when I call beforeEvent.rerouteToError(NullPointerException.class), then it works. But only by calling rerouteToError(…). Here is an example View that shows this.

@Route(value = "Test")
public class TestView extends VerticalLayout implements HasUrlParameter<String> {
    private static final Logger LOGGER = LogManager.getLogger(TestView.class);

    public TestView() {
        add(new Button("Cause NPE!", click -> {
            Object obj = null;
        add(new Button("Reroute to NPE handler", click -> {
            UI.getCurrent().navigate(TestView.class, "gogogo");

    public void setParameter(BeforeEvent beforeEvent, @OptionalParameter String s) {
        if(s != null && s.equals("gogogo")){

public class NPEHandler extends VerticalLayout implements HasErrorParameter<NullPointerException> {

    public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NullPointerException> parameter) {

        // general message about NPE
        add(new Label(getTranslation(NULLPOINTER_EXCEPTION_MSG.getKey())));

        // actual exception message
        add(new Label(parameter.getCaughtException().getMessage()));

        // exception cause
        StackTraceElement[] stackTrace = parameter.getException().getStackTrace();
        VerticalLayout stackTraceLayout = new VerticalLayout();
        for (StackTraceElement stackTraceElement : stackTrace) {
            stackTraceLayout.add(new Label(stackTraceElement.toString()));
        return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

Please advise if this is intentional, or how I can make it work. Thank you

Quoting https://github.com/vaadin/flow/issues/4549#issuecomment-591989816

HasErrorParameter is intended to be used when a view cannot be opened at all but not for exceptions from when the user interacts with the view.

After the view has been opened, it’s usually preferable to keep the view open also after unexpected exceptions so that the user would e.g. have a chance of copy pasting any unsaved changes to a separate document.

There is a separate mechanism for customizing the handling of those kinds of exceptions through VaadinSession::setErrorHandler.

Ah, yes I suspected something like this. Thank you for pointing me the right direction. Much appreciated