How to handle null state in return of event handlers for EventSourcedBehavior

I’m handling events with newEventHandlerBuilder(), and notice that if I return “null” for a state, the event will be handled by multiple handlers, for example, with this code

.onEvent(MemberDeleted.class, memberDeleted → {“forAnyState, onEvent: %s”, memberDeleted));
return null;
.onAnyEvent(event → {
logger.warning(String.format(“forAnyState, onAnyEvent: %s”, event));
return null;

I will see both the onEvent and onAnyEvent being called for a MemberDeleted event. But if i change the return type to a class (I added a NullMember class) I will only see the onEvent method called.

It’s not supported to return a null, if there is no real state to care about?

thanks in advance

The recommendation would be to either wrap the state in an Optional/Option and use empty/None for no state, or to encode empty as a type/class in you state representation. There is a section in the docs covering empty initial state that you can draw inspiration from here: Style Guide • Akka Documentation

1 Like