Onesignal-android-sdk: NullPointerException getSharedPreferences

Created on 5 Aug 2017  路  8Comments  路  Source: OneSignal/OneSignal-Android-SDK

This Exception is thrown in my react-native app.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at com.onesignal.OneSignal.getGcmPreferences(OneSignal.java:1672)
    at com.onesignal.OneSignalStateSynchronizer$UserState.loadState(OneSignalStateSynchronizer.java:366)
    at com.onesignal.OneSignalStateSynchronizer$UserState.<init>(OneSignalStateSynchronizer.java:246)
    at com.onesignal.OneSignalStateSynchronizer$UserState.<init>(OneSignalStateSynchronizer.java:233)
    at com.onesignal.OneSignalStateSynchronizer.getToSyncUserState(OneSignalStateSynchronizer.java:58)
    at com.onesignal.OneSignalStateSynchronizer.getRegistrationId(OneSignalStateSynchronizer.java:761)
    at com.onesignal.OneSignal.internalFireIdsAvailableCallback(OneSignal.java:1016)
    at com.onesignal.OneSignal.idsAvailable(OneSignal.java:998)
    at com.geektime.rnonesignalandroid.RNOneSignal.configure(RNOneSignal.java:97)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:345)
    at com.facebook.react.cxxbridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:141)
    at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
    at android.os.Looper.loop(Looper.java:159)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:196)
    at java.lang.Thread.run(Thread.java:761)
Medium Priority

Most helpful comment

This issue will be fixed in the next release through a thread executor which will execute queued up actions that were called before init

All 8 comments

@NikolaBorislavovHristov In this case idsAvailable is being called before OneSignal is initialized. We will correct this in the native SDK in an update to fire the callback once the SDK has been initialized.

Same for me and also within react-native app. Any progress, @jkasten2, @NikolaBorislavovHristov?

@hose314 I can't reproduce this.

@NikolaBorislavovHristov
For me its crashes when app just got installed within first launch

We're also getting this issue on a React Native app using the React Native package. I can't replicate it myself but logs from Google Play Developers Console show users experiencing a crash. Any workaround would be appreciated!

@NikolaBorislavovHristov @hose314 @nickpedersen we're currently working on a solution which will queue up OneSignal operations if the SDK hasn't been initialized. The operations will then be enacted in order upon SDK initialization. This should make operations such as idsAvailable safe to call without guaranteeing init.

This issue will be fixed in the next release through a thread executor which will execute queued up actions that were called before init

The 3.6.4 SDK is now released. Feel free to reopen this issue with an updated stack trace attached if the upgrade does not resolve the issue.

Was this page helpful?
0 / 5 - 0 ratings