In my app when a user sign out, it supposed to send a request to the server to indicate that the user has signed out and clean up stuff in server side.
It would make sense to send it after the user successfully signed out from Firebase. I'm using Firebase UI which has a signOut function, it returns a Task object that can be used to check if the user successfully logout, and send the request if it is successful.
But when I try to generate a token for the request by using getToken(), it gives me NullPointerException despite that the FirebaseUser object is not null.
public void signOut(View v) {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
serverCleanUp(); // make sure the user sign out successfully before sending to server
} else {
// handle error...
}
}
});
}
public void serverCleanUp() {
// mUser is the user object saved when signed in
if (mUser != null) {
mUser.getToken(true) // give me NullPointerException in this line
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
public void onComplete(@NonNull Task<GetTokenResult> task) {
// send request if success, handle error otherwise...
}
});
}
}
I think it is because when a user signed out, Firebase do not expect the user to generate token to perform request. But when I read the doc, it said that the Firebase User object will remain completely functional if there is a reference to it even after it signs out, does that mean everything is functional except getToken?
EDIT: This is not the nullPointerException caused by mUser
is null, because I make sure it is not null, and I try to call another FirebaseUser method, such as getEmail() right before getToken() to see is it work, and getEmail works, while getToken still throw a nullPointerException.
Stacktrace:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.internal.zzbmn com.google.firebase.auth.FirebaseUser.zzVI()' on a null object reference
at com.google.firebase.auth.FirebaseAuth.zza(Unknown Source)
at com.google.firebase.auth.FirebaseUser.getToken(Unknown Source)
at com.example.user.myapp.MainActivity.serverCleanUp(MainActivity.java:232)
at com.example.user.myapp.MainActivity.access$500(MainActivity.java:36)
at com.example.user.myapp.MainActivity$5.onComplete(MainActivity.java:219)
at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)