1

So I have a sign up function in my app, what it does is ask for an email and password and then creates an account with those parameters. Then the app logs the user with the newly created account and requests the email and uid that Firestore created, and puts it in a collection.

For whatever reason, the app crashes when I try to retrieve the account details from Firebase and when I check it, no account was ever created and no collection was made (Tho it never got to that point so I guess yeah).

What am I doing wrong?

This is the code (And no, for this project I don't need any username):

public class Register extends AppCompatActivity {
    EditText RegisterEditTextEmail, RegisterEditTextPassword, RegisterEditTextPasswordAgain;
    Button RegisterButton;
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    boolean emailGood = false, passwordGood = false, passwordMatch = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        RegisterButton = findViewById(R.id.RegisterButton);
        RegisterEditTextEmail = findViewById(R.id.RegisterEditTextEmail);
        RegisterEditTextPassword = findViewById(R.id.RegisterEditTextPassword);
        RegisterEditTextPasswordAgain = findViewById(R.id.RegisterEditTextPasswordAgain);
        RegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = RegisterEditTextEmail.getText().toString();
                String password = RegisterEditTextPassword.getText().toString();
                String passwordAgain = RegisterEditTextPasswordAgain.getText().toString();

               ........
               checking if the email and password are good
               ........

                if (emailGood && passwordGood && passwordMatch) {
                    auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
                            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(Task<AuthResult> task) {
                                    if(task.isComplete()){
                                        FirebaseUser user = auth.getCurrentUser();
                                        Map<String, Object> map = new HashMap<>();
                                        map.put("email", user.getEmail()); <---- crashes here
                                        map.put("uuid", user.getUid());
                                        db.collection("users")
                                                .document(user.getUid().toString())
                                                .set(map)
                                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                                    @Override
                                                    public void onSuccess(Void unused) {
                                                        Intent intent = new Intent(Register.this, MainActivity.class);
                                                        startActivity(intent);
                                                    }
                                                }).addOnFailureListener(new OnFailureListener() {
                                            @Override
                                            public void onFailure(@NonNull Exception e) {
                                                Toast.makeText(Register.this, e.getMessage().toString(), Toast.LENGTH_LONG);
                                            }
                                        });
                                    }
                                }
                            });

                }
            }
        });
    }
}

The error I get:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.clockin, PID: 3607
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference
        at com.example.clockin.Register$1$1.onComplete(Register.java:74)
        at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Edit: Someone suggested a NullPointer solution, I guess I failed to elaborate further. My problem isn't that the account is returned as null, my problem is that for whatever reason, FirebaseAuth refuses to create an account in the first place. And as a result, the account is null. Why won't FirebaseAuth make an account?

Edit 2: Apparently my API key in Google Cloud is expired, I tried looking around for a fix but didn't manage to find one, anyone knows how to fix it?

qwirrr
  • 31
  • 3
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Nov 13 '21 at 12:56
  • No, my problem is that for some reason Firebase refuses to create an account not that it's returning null. – qwirrr Nov 13 '21 at 13:37

1 Answers1

0

The problem is in this snippet:

auth.createUserWithEmailAndPassword(RegisterEditTextEmail.getText().toString(), RegisterEditTextPassword.getText().toString())
    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(Task<AuthResult> task) {
            if(task.isComplete()){
                FirebaseUser user = auth.getCurrentUser();
                Map<String, Object> map = new HashMap<>();
                map.put("email", user.getEmail());

When a Task completes it can either be successful or unsuccessful and you need to handle both case separately. Only when the task is successful will auth.getCurrentUser() have a value.

So you should only try to access that (and its getEmail) when the task is successful:

public void onComplete(Task<AuthResult> task) {
    if(task.isSuccessful()){ //  Change this test
        FirebaseUser user = auth.getCurrentUser();
        Map<String, Object> map = new HashMap<>();
        map.put("email", user.getEmail());

When the task failed, you can learn more about why it failed by logging the exception it contains. So:

public void onComplete(Task<AuthResult> task) {
    if(task.isSuccessful()){ //  Change this test
        FirebaseUser user = auth.getCurrentUser();
        Map<String, Object> map = new HashMap<>();
        map.put("email", user.getEmail());
        ...
    }
    else {
        Log.e("AUTH", "Creating user failed", task.getException()); // 
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807