launchdarkly / android-client-sdk

LaunchDarkly Client-side SDK for Android
Other
45 stars 23 forks source link

Fix NullPointerException when event buffer is full #160

Closed mattyway closed 2 years ago

mattyway commented 2 years ago

Requirements

Related issues

N/A

Describe the solution you've provided

When diagnosticOptOut is true and the events buffer is full, a null pointer exception will be thrown when trying to send an event. Example stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.launchdarkly.sdk.android.DiagnosticStore.incrementDroppedEventCount()' on a null object reference
at com.launchdarkly.sdk.android.LDClient.sendEvent(LDClient.java:668)
at com.launchdarkly.sdk.android.LDClient.identifyInternal(LDClient.java:314)
at com.launchdarkly.sdk.android.LDClient.identifyInstances(LDClient.java:335)
at com.launchdarkly.sdk.android.LDClient.identify(LDClient.java:301)
at com.launchdarkly.sdk.android.LDClientTest.testEventBufferFillsUp(LDClientTest.java:661)

This occurs because diagnosticStore is set to null when diagnosticOptOut is true (see here) and sendEvent() assumes diagnosticStore will not be null (see here)

Other usages of diagnosticStore are gated with if (diagnosticStore != null) to avoid throwing an exception (see here and here)

This pull request adds a similar gate to avoid the null pointer exception.

Describe alternatives you've considered

N/A

Additional context

N/A