{"record":{"id":"286d74b50112c250","repo":"greenrobot/EventBus","slug":"default-instance-already-exists-it-may-be-only-se","errorCode":null,"errorMessage":"Default instance already exists. It may be only set once before it's used the first time to ensure consistent behavior.","messagePattern":"Default instance already exists\\. It may be only set once before it's used the first time to ensure consistent behavior\\.","errorType":"exception","errorClass":"EventBusException","httpStatus":null,"severity":"error","filePath":"EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java","lineNumber":180,"sourceCode":"        if (mainThreadSupport != null) {\n            return mainThreadSupport;\n        } else if (AndroidComponents.areAvailable()) {\n            return AndroidComponents.get().defaultMainThreadSupport;\n        } else {\n            return null;\n        }\n    }\n\n    /**\n     * Installs the default EventBus returned by {@link EventBus#getDefault()} using this builders' values. Must be\n     * done only once before the first usage of the default EventBus.\n     *\n     * @throws EventBusException if there's already a default EventBus instance in place\n     */\n    public EventBus installDefaultEventBus() {\n        synchronized (EventBus.class) {\n            if (EventBus.defaultInstance != null) {\n                throw new EventBusException(\"Default instance already exists.\" +\n                        \" It may be only set once before it's used the first time to ensure consistent behavior.\");\n            }\n            EventBus.defaultInstance = build();\n            return EventBus.defaultInstance;\n        }\n    }\n\n    /** Builds an EventBus based on the current configuration. */\n    public EventBus build() {\n        return new EventBus(this);\n    }\n\n}\n","sourceCodeStart":162,"sourceCodeEnd":194,"githubUrl":"https://github.com/greenrobot/EventBus/blob/0194926b3bcf70cc0d7bfd3c5da16708dd5ab876/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java#L162-L194","documentation":"Thrown by EventBusBuilder.installDefaultEventBus() when EventBus.defaultInstance is already non-null. The default instance is a singleton installed once; installing a second builder configuration would make some consumers use the old config and others the new one, so EventBus refuses. Note the instance is also lazily created by EventBus.getDefault() itself, so merely calling getDefault() before installDefaultEventBus() triggers this too.","triggerScenarios":"Calling installDefaultEventBus() twice (e.g. from Application.onCreate() and again in a test setup); calling EventBus.getDefault() anywhere (field initializers, static init) before installDefaultEventBus(); a library and the app both installing a default bus.","commonSituations":"Android instrumentation tests that re-run Application onCreate; initializing EventBus in multiple modules; a static field like private EventBus bus = EventBus.getDefault() evaluated early in a class that loads before the custom builder install.","solutions":["Call installDefaultEventBus() exactly once, as the very first EventBus touchpoint (e.g. first lines of Application.onCreate())","Make sure no code path calls EventBus.getDefault() before the install, including static/field initializers and tests","If you need a different configuration later, build a separate non-default bus: EventBus bus = EventBus.builder()....build() and inject it explicitly","In tests, guard installation: if (EventBus.getDefault() == null) { builder.installDefaultEventBus(); }"],"exampleFix":"// before\nEventBus.getDefault().register(this); // lazy-creates default\n...\nEventBus.builder().logNoSubscriberMessages(false).installDefaultEventBus(); // throws\n\n// after\n// Application.onCreate(), before any getDefault() call\nEventBus.builder().logNoSubscriberMessages(false).installDefaultEventBus();\n// all later EventBus.getDefault() calls reuse the configured instance","handlingStrategy":"validation","validationCode":"// Install exactly once, before any getDefault() call\npublic class App extends Application {\n    @Override public void onCreate() {\n        super.onCreate();\n        EventBus.builder()\n            .throwSubscriberException(BuildConfig.DEBUG)\n            .installDefaultEventBus();\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    EventBus.builder().installDefaultEventBus();\n} catch (EventBusException e) {\n    if (String.valueOf(e.getMessage()).contains(\"Default instance already exists\")) {\n        // already installed (e.g. test re-entry): proceed with getDefault()\n    } else {\n        throw e;\n    }\n}","preventionTips":["Call installDefaultEventBus() as the first statement of Application.onCreate()","Audit for EventBus.getDefault() usages in static initializers that could run first","For alternate configurations, build separate EventBus instances instead of reinstalling the default"],"tags":["eventbus","singleton","initialization","configuration"],"backgroundTag":null,"analyzedSha":"0194926b3bcf70cc0d7bfd3c5da16708dd5ab876","analyzedAt":"2026-08-14T10:41:08.786Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}