Fix NullPointerException in AppLog.addEntry #145
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
This pull request addresses a NPE crash that could occur in the
AppLog.addEntry
method when attempting to callonLog
on a nullAppLogListener
object.Also contains a small ReadMe update for clarity.
Problem:
The
addEntry
method iterated through a list ofAppLogListener
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.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.