Home » c# » c# – Firebase Auth returns System.NullReference exception

c# – Firebase Auth returns System.NullReference exception

Posted by: admin February 21, 2020 Leave a comment

Questions:

I am new to this. When I try to create a user with email and password it tells me that my FirebaseAuth is null and doesn’t let me create a user.

I am initializing Firebase through this.

void InitializeFirebase()
        {
            var app = FirebaseApp.InitializeApp(this);

            if (app == null)
            {
                var options = new FirebaseOptions.Builder()
                    .SetApplicationId("")
                    .SetApiKey("")
                    .SetDatabaseUrl("")
                    .SetStorageBucket("")
                    .Build();

                app = FirebaseApp.InitializeApp(this, options);
                database = FirebaseDatabase.GetInstance(app);

                mAuth = FirebaseAuth.Instance;
            }
            else
            {
                database = FirebaseDatabase.GetInstance(app);

                mAuth = FirebaseAuth.Instance;
            }
        }

I am getting an error
System.NullReferenceException: ‘Object reference not set to an instance of an object.’
Which pops up here

mAuth.CreateUserWithEmailAndPassword(email, password)
.AddOnSuccessListener(this, TaskCompletionListener)
.AddOnFailureListener(this, TaskCompletionListener);
And registering user here:

void RegisterUser(string email, string password)
        {
            TaskCompletionListener.Success += TaskCompletionListener_Success;
            TaskCompletionListener.Failure += TaskCompletionListener_Failure;

            mAuth.CreateUserWithEmailAndPassword(email, password)
                .AddOnSuccessListener(this, TaskCompletionListener)
                .AddOnFailureListener(this, TaskCompletionListener);
        }

TaskCompletioListener


public class TaskCompletionListener : Java.Lang.Object, IOnSuccessListener, IOnFailureListener
    {
        public EventHandler Success;
        public EventHandler Failure;

        public void OnFailure(Java.Lang.Exception e)
        {
            Failure.Invoke(this, new EventArgs());
        }

        public void OnSuccess(Java.Lang.Object result)
        {
            Success.Invoke(this, new EventArgs());
        }
    }
How to&Answers: