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?