EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

E/EventBus: The event could not be sent: ClassX to the ClassX class java.lang.NullPointerException

Open gvoscar opened this issue 5 years ago • 3 comments

Translation: I am working with subscriptions to FirebaseRealTime and this happens when I make a query and attach a listener in parallel to the same FirebaseRealTime reference. This error does not block the user interface, because the events keep coming.

Original: Estoy trabajando con suscripciones a FirebaseRealTime y esto sucede cuando realizo una consulta y adjunto un oyente en paralelo a la misma referencia de FirebaseRealTime. Este error no me bloquea la interfaz de usuario, porque los eventos siguen llegando.

public class PresenterImpl implements Presenter {

    @Override
    public void onCreate() {
       // Registrarse.
        this.mEventBus.register(this);

        // Consulta
        this.mInteractor.loadInfo();
    }

    @Override
    public void onStart() {
        // Iniciar oyente
        this.mInteractor.subscribeChanges();
    }

    @Override
    public void onStop() {
        // Detener oyente.
        this.mInteractor.unSubscribeChanges();
    }

    @Override
    public void onDestroy() {
        // Anular registro.
        this.mEventBus.unregister(this);
    }

    @Override
    @Subscribe
    public void onEventReceived(Object event) {
            switch (event.getEventType()) {
                case ClasssX.loaded_info:
                    // Evento de consulta recibido.
                    break;
                case ClasssX.changes_info:
                    // Evento de oyente de cambios recibido.
                    break;
            }
    }

 }

Here in the LogCat the error appears but only when I start consulting and the listener at the first time, after the listener events are received normally.

In the repository I post the event normally.

Original: Aquí en el LogCat me aparece el error pero solo cuando inicio la consultar y el oyente en la primera vez, despues los eventos de oyente se reciben con normalidad.

En el repositorio posteo el evento normalmente.

E/EventBus: Could not dispatch event: class com.xxx.events.ClassEvent to subscribing class class com.xxx.presenters.PresenterImpl java.lang.NullPointerException

at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:507) at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:430) at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:411) at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:384) at org.greenrobot.eventbus.EventBus.post(EventBus.java:265) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)

gvoscar avatar Jul 10 '19 16:07 gvoscar

This error indicates that the code of the @Subscribe method in PresenterImpl produces a java.lang.NullPointerException.

Anyhow, I recommend against using Object event and instead adding an event method for each type of event. For example @Subscribe public void onEventReceived(ClassEvent event).

greenrobot-team avatar Jul 16 '19 07:07 greenrobot-team

Hello greenrobot-team

I handle the events with a specific class and all its variables encapsulated, in the code I wrote (Objetc event) as an example, but I receive events in this way. @Subscribe public void onEventReceived (ClassEvent event)

Only that the ecepcion does not block the main thread, but I do not have any try-catch in the presenter. This error is internal to EventBus.

I think this happens because the subscription to FirebaseRealTime is so fast that it locks the instance of EventBus for the same type of event.

Multiple events are received simultaneously, so I handle two listeners with the same event.

Before publishing the event I check that no data is null, in addition to the ClassEvent

gvoscar avatar Jul 17 '19 22:07 gvoscar

when register ,eventbus will post sticky events then execute the subscribeevents

but u register in onCreate which the UI variables (like TextView and EditText) has not been initialized

so it would show NullPointerException.

that 's why eventbus team encourage us to register in onStart :)

tomridder avatar Mar 16 '23 07:03 tomridder