0

I'm trying to check if the user is already signed in to google in kotlin.

fun isSignedIn(): Boolean {
        println(FirebaseAuth.getInstance().currentUser)
        println(GoogleSignIn.getLastSignedInAccount(this))
        return GoogleSignIn.getLastSignedInAccount(this) != null
    }

However I'm not sure why I'm getting the null pointer exception for the line GoogleSignIn.getLastSignedInAccount(this):

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

I've been looking at other stackoverflow threads and following what has been suggested, but I'm not sure what the problem is.

  1. https://stackoverflow.com/questions/38190253/android-google-sign-in-check-if-user-is-signed-in#:~:text=To%20check%20if%20the%20user%20is%20signed-in%2C%20call%20isConnected,isConnected().

Am I overlooking something very simple? Thank you.

Mark
  • 3,138
  • 5
  • 19
  • 36

1 Answers1

0

Not sure why I'm getting the null pointer exception but if anyone else encounters the same problem, it seems:

FirebaseAuth.getInstance().currentUser != null

works fine too.

FirebaseAuth.getInstance().currentUser!!.displayName

gets the user name, where as

FirebaseAuth.getInstance().currentUser!!.uid

gets the uid

Mark
  • 3,138
  • 5
  • 19
  • 36