Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullPointerException in AppLog.addEntry #145

Merged

Conversation

backwardstruck
Copy link
Contributor

@backwardstruck backwardstruck commented Mar 15, 2024

Description:

This pull request addresses a NPE crash that could occur in the AppLog.addEntry method when attempting to call onLog on a null AppLogListener object.

Also contains a small ReadMe update for clarity.

Problem:

The addEntry method iterated through a list of AppLogListener objects without checking for null values. If no listeners were registered, the loop would attempt to call methods on null objects, leading to an NPE.

NullPointerException: Attempt to invoke interface method 'void org.wordpress.android.util.AppLog$AppLogListener.onLog(org.wordpress.android.util.AppLog$T, org.wordpress.android.util.AppLog$LogLevel, java.lang.String)' on a null object reference
    at org.wordpress.android.util.AppLog.addEntry(AppLog.java:308)
    at org.wordpress.android.util.AppLog.d(AppLog.java:139)
    at org.wordpress.android.fluxc.utils.AppLogWrapper.d(AppLogWrapper.kt:8)
    at org.wordpress.android.fluxc.tools.CoroutineEngine.launch(CoroutineEngine.kt:60)
    at org.wordpress.android.fluxc.store.EncryptedLogStore.onAction(EncryptedLogStore.kt:79)
...
(7 additional frame(s) were not displayed)

EventBusException: Invoking subscriber failed
    at org.greenrobot.eventbus.EventBus.handleSubscriberException(EventBus.java:537)
    at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:519)
    at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:511)
    at org.greenrobot.eventbus.AsyncPoster.run(AsyncPoster.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
...
(2 additional frame(s) were not displayed)

Solution:

Thanks for the help figuring out what to do here @jd-alexander
I added a simple null check within the loop to ensure we only call onLog on valid listeners. If a null listener is encountered, a warning message is logged to indicate the issue.

It should be noted that the root problem still remains as I was unable to identify it. i.e. why was the listener null in the first place. This solution just addresses the symptom (NPE), not the underlying problem of the absence of the listener.

Notes:

An alternative approach would be to filter out null listeners before iterating. I did not implemented in this PR due to a preference for simpler code but I'd be happy to change it if we feel that's more performant.

@notandyvee
Copy link
Contributor

I don't really have a problem with this fix. Reasonable to null check. The problem is when adding an entry it's tagged as @NonNull. Any idea how that's possible?

@backwardstruck
Copy link
Contributor Author

I don't really have a problem with this fix. Reasonable to null check. The problem is when adding an entry it's tagged as @NonNull. Any idea how that's possible?

Thanks for the review! That's a good question. Something doesn't add up. Maybe that's pointing to the problem being somewhere else.

Copy link
Contributor

@notandyvee notandyvee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change LGTM! I'll let you merge so you can cut the release whenever you are ready @backwardstruck .

@backwardstruck backwardstruck merged commit f2f9795 into trunk Aug 19, 2024
8 checks passed
@backwardstruck backwardstruck deleted the fix/eventbus_exception_invoking_subscriber_failed branch August 19, 2024 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants