greenrobot/EventBus · error · EventBusException

Default instance already exists. It may be only set once bef

Error message

Default instance already exists. It may be only set once before it's used the first time to ensure consistent behavior.

What it means

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.

Source

Thrown at EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java:180

        if (mainThreadSupport != null) {
            return mainThreadSupport;
        } else if (AndroidComponents.areAvailable()) {
            return AndroidComponents.get().defaultMainThreadSupport;
        } else {
            return null;
        }
    }

    /**
     * Installs the default EventBus returned by {@link EventBus#getDefault()} using this builders' values. Must be
     * done only once before the first usage of the default EventBus.
     *
     * @throws EventBusException if there's already a default EventBus instance in place
     */
    public EventBus installDefaultEventBus() {
        synchronized (EventBus.class) {
            if (EventBus.defaultInstance != null) {
                throw new EventBusException("Default instance already exists." +
                        " It may be only set once before it's used the first time to ensure consistent behavior.");
            }
            EventBus.defaultInstance = build();
            return EventBus.defaultInstance;
        }
    }

    /** Builds an EventBus based on the current configuration. */
    public EventBus build() {
        return new EventBus(this);
    }

}

View on GitHub (pinned to 0194926b3b)

Solutions

  1. Call installDefaultEventBus() exactly once, as the very first EventBus touchpoint (e.g. first lines of Application.onCreate())
  2. Make sure no code path calls EventBus.getDefault() before the install, including static/field initializers and tests
  3. If you need a different configuration later, build a separate non-default bus: EventBus bus = EventBus.builder()....build() and inject it explicitly
  4. In tests, guard installation: if (EventBus.getDefault() == null) { builder.installDefaultEventBus(); }

Example fix

// before
EventBus.getDefault().register(this); // lazy-creates default
...
EventBus.builder().logNoSubscriberMessages(false).installDefaultEventBus(); // throws

// after
// Application.onCreate(), before any getDefault() call
EventBus.builder().logNoSubscriberMessages(false).installDefaultEventBus();
// all later EventBus.getDefault() calls reuse the configured instance
Defensive patterns

Strategy: validation

Validate before calling

// Install exactly once, before any getDefault() call
public class App extends Application {
    @Override public void onCreate() {
        super.onCreate();
        EventBus.builder()
            .throwSubscriberException(BuildConfig.DEBUG)
            .installDefaultEventBus();
    }
}

Try / catch

try {
    EventBus.builder().installDefaultEventBus();
} catch (EventBusException e) {
    if (String.valueOf(e.getMessage()).contains("Default instance already exists")) {
        // already installed (e.g. test re-entry): proceed with getDefault()
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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