java.lang.NullPointerException: FirebaseInstanceId.getInstance() return null
Environment:
- Android Studio version: 3.6.3
- Firebase Component: Firebase Cloud Messaging
- Component version: 20.1.7
Problem
I recently got NPE crashes in Crashlytics report cause by FirebaseInstanceId.getInstance():
"Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.b()' on a null object reference"
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, instanceIdResult->{});
I found a few problems with this issue:
- I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
- This issue does not seem to follow the issue template. Make sure you provide all the required information.
Thanks for reporting. We need some help from you to triage this
- Full stack trace from Crashlytics
- Are the crashes happening on specific devices, android versions etc?
- Please share the dependencies you are using i.e relevant parts of your gradle file or
./gradlew :app:dependencies.
Hey @NhatPham94. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
This should only be happening if the default FirebaseApp hasn't been initialized yet. Are you using the FirebaseInitProvider for initialization? If not then you probably need to throw in a FirebaseApp.initializeApp(myContext) before you call into FirebaseInstanceId.
Sorry for late response.
Devices : itel P33, Samsung Note3, Samsung Galaxy J2 Pro, Infinix x572, TECNO WX3, Xiaomi Mi 4c, Huawei JNY-LX1 Android : 5.0, 6.0.1, 7.0, 8.1.0, 10 App A : 61 crashes, 4 users affected App B : 41 crashes, 3 users affected ...
+--- com.google.firebase:firebase-messaging:20.1.7 | +--- androidx.collection:collection:1.0.0 -> 1.1.0 () | +--- androidx.core:core:1.0.0 -> 1.1.0 () | +--- com.google.android.datatransport:transport-api:2.2.0 | +--- com.google.android.datatransport:transport-backend-cct:2.2.0 | | +--- androidx.annotation:annotation:1.1.0 | | +--- com.google.android.datatransport:transport-api:2.2.0 | | +--- com.google.android.datatransport:transport-runtime:2.2.0 | | | +--- androidx.annotation:annotation:1.1.0 | | | +--- com.google.android.datatransport:transport-api:2.2.0 | | | --- com.google.dagger:dagger:2.24 -> 2.27 | | | --- javax.inject:javax.inject:1 | | --- com.google.firebase:firebase-encoders-json:16.0.0 | | --- androidx.annotation:annotation:1.1.0 | +--- com.google.android.datatransport:transport-runtime:2.2.0 () | +--- com.google.android.gms:play-services-basement:17.0.0 -> 17.1.1 () | +--- com.google.android.gms:play-services-stats:17.0.0 () | +--- com.google.android.gms:play-services-tasks:17.0.0 () | +--- com.google.firebase:firebase-common:19.3.0 | | +--- com.google.android.gms:play-services-basement:17.0.0 -> 17.1.1 () | | +--- com.google.android.gms:play-services-tasks:17.0.0 () | | +--- com.google.auto.value:auto-value-annotations:1.6.5 | | --- com.google.firebase:firebase-components:16.0.0 | | --- androidx.annotation:annotation:1.1.0 | +--- com.google.firebase:firebase-components:16.0.0 () | +--- com.google.firebase:firebase-datatransport:17.0.3 | | +--- androidx.annotation:annotation:1.1.0 | | +--- com.google.android.datatransport:transport-api:2.1.0 -> 2.2.0 | | +--- com.google.android.datatransport:transport-backend-cct:2.1.0 -> 2.2.0 () | | +--- com.google.android.datatransport:transport-runtime:2.1.0 -> 2.2.0 () | | --- com.google.firebase:firebase-common:19.3.0 () | +--- com.google.firebase:firebase-encoders-json:16.0.0 () | +--- com.google.firebase:firebase-iid:[20.1.7] -> 20.1.7 | | +--- androidx.collection:collection:1.0.0 -> 1.1.0 () | | +--- androidx.core:core:1.0.0 -> 1.1.0 () | | +--- androidx.legacy:legacy-support-core-utils:1.0.0 () | | +--- com.google.android.gms:play-services-basement:17.0.0 -> 17.1.1 () | | +--- com.google.android.gms:play-services-stats:17.0.0 () | | +--- com.google.android.gms:play-services-tasks:17.0.0 () | | +--- com.google.firebase:firebase-common:19.3.0 () | | +--- com.google.firebase:firebase-components:16.0.0 () | | +--- com.google.firebase:firebase-iid-interop:17.0.0 | | | +--- com.google.android.gms:play-services-base:17.0.0 -> 17.1.0 () | | | --- com.google.android.gms:play-services-basement:17.0.0 -> 17.1.1 () | | +--- com.google.firebase:firebase-installations:16.3.0 | | | +--- com.google.android.gms:play-services-tasks:17.0.0 () | | | +--- com.google.firebase:firebase-common:19.3.0 () | | | +--- com.google.firebase:firebase-components:16.0.0 () | | | --- com.google.firebase:firebase-installations-interop:16.0.0 | | | --- com.google.android.gms:play-services-tasks:17.0.0 () | | --- com.google.firebase:firebase-installations-interop:16.0.0 () | +--- com.google.firebase:firebase-installations:16.3.0 () | +--- com.google.firebase:firebase-installations-interop:16.0.0 () | --- com.google.firebase:firebase-measurement-connector:18.0.0 | --- com.google.android.gms:play-services-basement:17.0.0 -> 17.1.1 (*)
About versions, I only update libs version but does not change any code. It's not happening within the last version com.google.firebase:firebase-messaging:20.1.0
Init in onCreate() within CustomApplication class : FirebaseApp.initializeApp(this);
protected void initGCM() {
FirebaseInstanceId instanceId = FirebaseInstanceId.getInstance();
if (instanceId != null) {
TaskinstanceId.getInstanceId() return null but it's not, it's actually from FirebaseInstanceId.getInstance() instead. I have to add NPE checking to hot fix this issue.
Stack Trace
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.iid.FirebaseInstanceId.b()' on a null object reference
at com.mingle.twine.activities.BaseLogOnActivity.initGCM(BaseLogOnActivity.java:100)
at com.mingle.twine.activities.BaseLogOnActivity.lambda$afterAuthenticate$348(BaseLogOnActivity.java:148)
at com.mingle.twine.activities.-$$Lambda$BaseLogOnActivity$alrBHkkSawwTfzeU5SCgr9alMDM.accept(-.java:6)
at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:62)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:124)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6765)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I removed firebase-installations from Labels.
I don't think that this is related to FIS.
@ciarand : Please, feel free to reassign to us.!
Any info about this issue? Thanks.
Hi @NhatPham94 Sorry for the long wait here. I'd like to confirm if you're still seeing this issue? I'd recommend using the latest SDK version in your app to ensure old known bugs are fixed.
Thanks!
Hey @NhatPham94. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Since there haven't been any recent updates here, I am going to close this issue.
@NhatPham94 if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.