greenrobot/EventBus · error · EventBusException

Event may not be null

Error message

Event may not be null

What it means

Thrown by cancelEventDelivery(Object) when the argument is null. The API needs the exact event instance currently being dispatched to verify (by reference equality, postingState.event != event) that the caller is aborting the in-flight event; a null argument cannot be matched, so EventBus rejects it immediately after the isPosting check. This is a plain argument-validation failure by the caller.

Source

Thrown at EventBus/src/org/greenrobot/eventbus/EventBus.java:297

                postingState.isMainThread = false;
            }
        }
    }

    /**
     * Called from a subscriber's event handling method, further event delivery will be canceled. Subsequent
     * subscribers
     * won't receive the event. Events are usually canceled by higher priority subscribers (see
     * {@link Subscribe#priority()}). Canceling is restricted to event handling methods running in posting thread
     * {@link ThreadMode#POSTING}.
     */
    public void cancelEventDelivery(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        if (!postingState.isPosting) {
            throw new EventBusException(
                    "This method may only be called from inside event handling methods on the posting thread");
        } else if (event == null) {
            throw new EventBusException("Event may not be null");
        } else if (postingState.event != event) {
            throw new EventBusException("Only the currently handled event may be aborted");
        } else if (postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING) {
            throw new EventBusException(" event handlers may only abort the incoming event");
        }

        postingState.canceled = true;
    }

    /**
     * Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky
     * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.
     */
    public void postSticky(Object event) {
        synchronized (stickyEvents) {
            stickyEvents.put(event.getClass(), event);
        }
        // Should be posted after it is putted, in case the subscriber wants to remove immediately

View on GitHub (pinned to 0194926b3b)

Solutions

  1. Pass the exact event object received in the @Subscribe method parameter: cancelEventDelivery(event)
  2. Add a null check before calling if the event reference is not guaranteed to be set

Example fix

// before
EventBus.getDefault().cancelEventDelivery(pendingEvent); // pendingEvent == null

// after
@Subscribe(threadMode = ThreadMode.POSTING)
public void onEvent(MessageEvent event) {
    EventBus.getDefault().cancelEventDelivery(event);
}
Defensive patterns

Strategy: validation

Validate before calling

if (event == null) {
    throw new IllegalArgumentException("event must not be null");
}
EventBus.getDefault().cancelEventDelivery(event);

Prevention

When it happens

Trigger: Calling cancelEventDelivery(null); calling it with an event variable that was never assigned or was cleared; passing a boxed/optional value that unwraps to null.

Common situations: Defensive code paths that pass an event field which is null when no event has been received yet; refactoring that renames the parameter and accidentally passes the wrong (null) variable.

Related errors


AI-assisted analysis of greenrobot/EventBus@0194926b3b (2026-08-14). Data as JSON: /api/errors/acd1ee86e45d8c54. Report an issue: GitHub.