user3502626
user3502626

Reputation: 838

Java.Lang.NullPointerException: Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.zza

I'm trying to implement Authenticate with Firebase on Android using a Phone Number by following the Google official documentation on Xamarin Android. But I am getting this error:

Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'void com.google.firebase.auth.FirebaseAuth.zza(java.lang.String, long, java.util.concurrent.TimeUnit, com.google.firebase.auth.PhoneAuthProvider$OnVerificationStateChangedCallbacks, android.app.Activity, java.util.concurrent.Executor, boolean)' on a null object reference'

I created an Android studio project And I got no error sending sms. I was supposed to publish the app yesterday but I'm stuck here.

Note: I have already a reference to Xamarin.GooglePlayServices.Base, Xamarin.Firebase.Auth:71.1605.0 and I have a code with push notification FCM which is working fine.

Here is my MainActivity.OnCreate code:

        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());

        Xamarin.FormsMaps.Init(this, savedInstanceState);

        FirebaseApp firebaseApp = FirebaseApp.InitializeApp(this);

        var firebaseAuthInstance = FirebaseAuth.GetInstance(firebaseApp);

        if(firebaseAuthInstance == null)
        {
            firebaseAuthInstance = new FirebaseAuth(firebaseApp);
        }

        verificationStateChangedCallbacks = new VerificationStateChangedCallbacks(); //This class is below.

        PhoneAuthProvider.Instance.VerifyPhoneNumber("+16505559999", 60, TimeUnit.Seconds, this //The activity, verificationStateChangedCallbacks); // I got the error here

Class VerificationStateChangedCallbacks:

public class VerificationStateChangedCallbacks : OnVerificationStateChangedCallbacks
    {
        public override void OnVerificationCompleted(PhoneAuthCredential p0)
        {
        }

        public override void OnVerificationFailed(FirebaseException p0)
        {
        }

        public override void OnCodeSent(string p0, ForceResendingToken p1)
        {
            base.OnCodeSent(p0, p1);
        }
    }

Upvotes: 0

Views: 380

Answers (2)

user3502626
user3502626

Reputation: 838

I just fixed it by changing

PhoneAuthProvider.Instance.VerifyPhoneNumber("+16505559999", 60, TimeUnit.Seconds, this, verificationStateChangedCallbacks);

to

PhoneAuthProvider.GetInstance(firebaseAuthInstance).VerifyPhoneNumber("+16505559999", 60, TimeUnit.Seconds, this, verificationStateChangedCallbacks);

2 days and I didn't think about changing it.

Upvotes: 1

user13431110
user13431110

Reputation:

can you check whether "PhoneAuthProvider.Instance" a null object. If yes then please check your file "google-services.json". Normally "FirebaseAuth.GetInstance(firebaseApp)" will not return a null.

Upvotes: 0

Related Questions