Getting NPE in FirebaseDatabase.getReference()

Viewed 342

I am getting NPE in production build on app start and only once after reinstalling with adb.

Caused by java.lang.NullPointerException
Attempt to invoke interface method 'void com.google.firebase.database.obfuscated.f.a(java.lang.Runnable)' on a null object reference
com.google.firebase.database.obfuscated.zzab.zza (SourceFile:276)
com.google.firebase.database.obfuscated.zzab. (SourceFile:90)
com.google.firebase.database.obfuscated.zzad.zzb (SourceFile:101)
com.google.firebase.database.obfuscated.zzad.zza (SourceFile:42)
com.google.firebase.database.FirebaseDatabase.zza (SourceFile:357)
com.google.firebase.database.FirebaseDatabase.getReference(SourceFile:201)

I suppose I should not get NPE no matter what reference I am passing to getReference() method - although I am passing correct one.

This seems to happen with following firebase versions:

firebase_core_version = "16.0.3"
firebase_auth_version = "16.0.3"
firebase_database_version = "16.0.2"

I am unable to reproduce it using:

firebase_core_version = "16.0.0"
firebase_auth_version = "16.0.1"
firebase_database_version = "16.0.1"
3 Answers

I downgraded firebase-ui-database from 4.2.0 to 4.1.0 and the problem went away

add this to the ProGuard configuration, in order to exclude these classes from obfuscation:

-keep,includedescriptorclasses class com.google.firebase.** { *; }

the fact, that only the release build is being affected hints for this -

different library versions may provide different (library) consumer rules.

I found solution that actually work's for me in this link

The FirebaseMessagingService is causing me the issue. So, to reproduce, add:

Manifest:

<service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
</service>

MyFirebaseMessagingService.class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onNewToken(String token) {

    try{
        DatabaseReference fRef = FirebaseDatabase.getInstance().getReference();
        //  I am getting the reference to write the token in firebase database like:
        // fRef.child("token").setValue(token);
    } catch (Exception e) {
        Log.d("MyFirebaseMsgService", e.getStackTrace().toString());
    }
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

}
}

In firebase-database 16.0.2, the line DatabaseReference fRef = FirebaseDatabase.getInstance().getReference(); is firing an exception:

Can't create handler inside thread that has not called Looper.prepare()

whereas in 16.0.1 it wasn't. When this exception is fired, the next firebaseDatabase.getReference() is crashing with the exception described initially.

Dependencies:

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:27.1.1'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-messaging:17.3.1'
implementation 'com.google.firebase:firebase-invites:16.0.3'
implementation 'com.firebaseui:firebase-ui-auth:4.2.0'

implementation 'com.firebaseui:firebase-ui-database:4.2.0'
//    implementation('com.firebaseui:firebase-ui-database:4.2.0') {
//        exclude group: 'com.google.firebase', module: 'firebase-database'
//    }
//    implementation 'com.google.firebase:firebase-database:16.0.1'
}
Related