launchdarkly/react-native-client-sdk

NPE on getLastFailedConnection since it do null.intValue()

dprevost-LMI opened this issue · 8 comments

Is this a support request?
I guess not

Describe the bug
We are having some inconsistent issues where the allFlags return an empty object and to get more information, I tried to use the getLastFailedConnection to see if there is any failure. Still, I guess there is not since it is doing an NPE.

Since getLastFailedConnection() is null clearly the below will be a NPE....

promise.resolve(LDClient.getForMobileKey(environment).getConnectionInformation().getLastFailedConnection().intValue());

To reproduce
Call getLastFailedConnection without any failure you will get a NPE

Expected behavior
Return null as mentioned in the doc

        /**
         * Returns the most recent successful flag cache update in millis from the epoch
         * or null if flags have never been retrieved.
         *
         * @param environment
         *   Optional string to execute the function in a different environment than the default.
         * @returns 
         *   A promise containing a number representing the status of the connection to LaunchDarkly, 
         *   or null if a successful connection has yet to be established.
         */
        getLastSuccessfulConnection(environment?: string): Promise<number | null>;

Logs

 Attempt to invoke virtual method 'int java.lang.Long.intValue()' on a null object reference

SDK version
The version of this SDK that you are using.

Language version, developer tools
React native

OS/platform
Mac

Additional context

Hello @dprevost-LMI, thank you for reporting the issue you see.

Given the logs said java, can you confirm that the issue is happening for android? If you use iOS, do you see this issue also for iOS?

Filed internally as 173523.

I don't think that part of the code had changed so we will need to test this for 7.x and see what is the current state.

Hello @dprevost-LMI, thank you for reporting the issue you see.

Given the logs said java, can you confirm that the issue is happening for android? If you use iOS, do you see this issue also for iOS?

Filed internally as 173523.

Yes, it was for Android. I did not test for iOS, but by code inspection, you can easily find that a null not checked can result in a NPE

Hello @dprevost-LMI, do you mind letting me know which version of the SDK you are using? The Exception handling for that method is updated in the RN SDK 7.x - the behavior is now different.

The Type doc is still not accurate though, in 7.x, when there is no failure for getLastFailedConnection (or success for getLastSuccessfulConnection in the document you quoted), it is returning a resolved promise with value 0 instead of null.

Hey!

As you can see in this code, the lastFailedConnnection can be set to null.

So it would be just natural to do a null check in that code

So the fix would be:

const lastFailedConnection  = LDClient.getForMobileKey(environment).getConnectionInformation().getLastFailedConnection();
promise.resolve(lastFailedConnection == null ? null: lastFailedConnection.intValue() );

Or, as you said, change the doc there

Note: you have the same problem for LastSuccessfulConnection which I guess does not happen often

@louis-launchdarkly you need more from me?

Hello @dprevost-LMI, thank you for the reply - sorry that there is an issue about the workflow (which should have remove the waiting for feedback label). I will discuss with the JS engineer on what is the behavior the SDK should do.

@dprevost-LMI we are fixing the null error and will release a new version soon. Thank you for reporting this.