{"record":{"id":"acd1ee86e45d8c54","repo":"greenrobot/EventBus","slug":"event-may-not-be-null","errorCode":null,"errorMessage":"Event may not be null","messagePattern":"Event may not be null","errorType":"exception","errorClass":"EventBusException","httpStatus":null,"severity":"error","filePath":"EventBus/src/org/greenrobot/eventbus/EventBus.java","lineNumber":297,"sourceCode":"                postingState.isMainThread = false;\n            }\n        }\n    }\n\n    /**\n     * Called from a subscriber's event handling method, further event delivery will be canceled. Subsequent\n     * subscribers\n     * won't receive the event. Events are usually canceled by higher priority subscribers (see\n     * {@link Subscribe#priority()}). Canceling is restricted to event handling methods running in posting thread\n     * {@link ThreadMode#POSTING}.\n     */\n    public void cancelEventDelivery(Object event) {\n        PostingThreadState postingState = currentPostingThreadState.get();\n        if (!postingState.isPosting) {\n            throw new EventBusException(\n                    \"This method may only be called from inside event handling methods on the posting thread\");\n        } else if (event == null) {\n            throw new EventBusException(\"Event may not be null\");\n        } else if (postingState.event != event) {\n            throw new EventBusException(\"Only the currently handled event may be aborted\");\n        } else if (postingState.subscription.subscriberMethod.threadMode != ThreadMode.POSTING) {\n            throw new EventBusException(\" event handlers may only abort the incoming event\");\n        }\n\n        postingState.canceled = true;\n    }\n\n    /**\n     * Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky\n     * event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.\n     */\n    public void postSticky(Object event) {\n        synchronized (stickyEvents) {\n            stickyEvents.put(event.getClass(), event);\n        }\n        // Should be posted after it is putted, in case the subscriber wants to remove immediately","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/greenrobot/EventBus/blob/0194926b3bcf70cc0d7bfd3c5da16708dd5ab876/EventBus/src/org/greenrobot/eventbus/EventBus.java#L279-L315","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the exact event object received in the @Subscribe method parameter: cancelEventDelivery(event)","Add a null check before calling if the event reference is not guaranteed to be set"],"exampleFix":"// before\nEventBus.getDefault().cancelEventDelivery(pendingEvent); // pendingEvent == null\n\n// after\n@Subscribe(threadMode = ThreadMode.POSTING)\npublic void onEvent(MessageEvent event) {\n    EventBus.getDefault().cancelEventDelivery(event);\n}","handlingStrategy":"validation","validationCode":"if (event == null) {\n    throw new IllegalArgumentException(\"event must not be null\");\n}\nEventBus.getDefault().cancelEventDelivery(event);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the subscriber method's event parameter straight through","Fail fast on null event references with your own clearer error"],"tags":["eventbus","null-check","cancellation","validation"],"backgroundTag":null,"analyzedSha":"0194926b3bcf70cc0d7bfd3c5da16708dd5ab876","analyzedAt":"2026-08-14T10:41:08.786Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}