greenrobot/EventBus · error · EventBusException

@Subscribe method ${methodName}must have exactly 1 parameter

Error message

@Subscribe method ${methodName}must have exactly 1 parameter but has ${parameterTypes.length}

What it means

Thrown during method verification in SubscriberMethodFinder when strictMethodVerification is enabled (EventBusBuilder.strictMethodVerification, default true) and a method carries @Subscribe but does not have exactly one parameter. EventBus dispatches a single event object per call, so a subscriber method must declare exactly one event-type parameter; zero or 2+ parameters cannot be wired. Note the message text has a missing space: '...methodName must have exactly 1 parameter but has N'.

Source

Thrown at EventBus/src/org/greenrobot/eventbus/SubscriberMethodFinder.java:186

            findState.skipSuperClasses = true;
        }
        for (Method method : methods) {
            int modifiers = method.getModifiers();
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1) {
                    Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                    if (subscribeAnnotation != null) {
                        Class<?> eventType = parameterTypes[0];
                        if (findState.checkAdd(method, eventType)) {
                            ThreadMode threadMode = subscribeAnnotation.threadMode();
                            findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                        }
                    }
                } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
                    String methodName = method.getDeclaringClass().getName() + "." + method.getName();
                    throw new EventBusException("@Subscribe method " + methodName +
                            "must have exactly 1 parameter but has " + parameterTypes.length);
                }
            } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
                String methodName = method.getDeclaringClass().getName() + "." + method.getName();
                throw new EventBusException(methodName +
                        " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
            }
        }
    }

    static void clearCaches() {
        METHOD_CACHE.clear();
    }

    static class FindState {
        final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
        final Map<Class, Object> anyMethodByEventType = new HashMap<>();
        final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();

View on GitHub (pinned to 0194926b3b)

Solutions

  1. Change the method to take exactly one event parameter: @Subscribe public void onEvent(MessageEvent event)
  2. If two pieces of data are needed, wrap them in a single event class holding both fields
  3. Remove the @Subscribe annotation from methods that are not handlers

Example fix

// before
@Subscribe
public void onEvent(MessageEvent event, Context ctx) { ... } // 2 params -> throws

// after
public class MessageEvent { public final String text; public final Context ctx; ... }

@Subscribe
public void onEvent(MessageEvent event) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// CI check: every @Subscribe method has exactly one parameter
for (Method m : subscriberClass.getDeclaredMethods()) {
    Subscribe ann = m.getAnnotation(Subscribe.class);
    if (ann != null && m.getParameterCount() != 1) {
        throw new IllegalStateException("@Subscribe method " + m + " must have exactly 1 parameter");
    }
}

Prevention

When it happens

Trigger: Declaring @Subscribe void onEvent() with no args, or @Subscribe void onEvent(A a, B b) with two; enabling strictMethodVerification and registering a class containing such a method; the check fires during register() while scanning the class.

Common situations: Copy-paste of a regular callback into a subscriber; adding a second 'context' or 'tag' parameter to a handler; migrating from other bus libraries that allow extra parameters; leftover debug overloads annotated by accident.

Related errors


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