1

I am trying to add user to the real-time database in Firebase using their auth UID as the child node.

I have been using this question as a guide:

https://stackoverflow.com/questions/40000899/how-to-link-between-authenticated-users-and-database-in-firebase

The users get added when I run the project in debug mode. The app crashes however when I run it normally. The error I am getting is a null pointer exception.

Here is my code:

public class RegisterUser extends AppCompatActivity {

    private static final String TAG = "RegisterUser";

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseUser fbUser =null;
    private DatabaseReference mFirebaseDatabase;
    User user;

    private EditText mEmail, mPassword;
    private RadioButton rManager, rPlayer;
    private Button mRegister;
    private ProgressBar mProgressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);

        mEmail = (EditText) findViewById(R.id.email_field);
        mPassword = (EditText) findViewById(R.id.password_field);
        rManager = (RadioButton) findViewById(R.id.managerBtn);
        rPlayer = (RadioButton) findViewById(R.id.playerBtn);
        mRegister = (Button) findViewById(R.id.btnSignIn);
        mProgressbar = (ProgressBar) findViewById(R.id.progressBar);

        mAuth = FirebaseAuth.getInstance();


        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String email = mEmail.getText().toString();
                String password = mPassword.getText().toString();
                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.length() < 6) {
                    Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!rManager.isChecked() && !rPlayer.isChecked()) {
                    Toast.makeText(RegisterUser.this, "You must select either player or manager", Toast.LENGTH_LONG).show();
                }

                mProgressbar.setVisibility(View.VISIBLE);
                String type = "";
                //radio buttons
                if (rManager.isChecked()) {
                    type = "Manager";
                } else if (rPlayer.isChecked()) {
                    type = "Player";
                }
                mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
                String userId = mFirebaseDatabase.push().getKey();
                User user = new User(email, password, type);
                mAuth.createUserWithEmailAndPassword(email, password);
                fbUser = FirebaseAuth.getInstance().getCurrentUser();
                mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
                mProgressbar.setVisibility(View.GONE);
                startActivity(new Intent(RegisterUser.this, HomePage.class));


            @Override
            protected void onResume() {
                super.onResume();
                mProgressbar.setVisibility(View.GONE);
            }

            public void onLogIn(View v) {
                Intent i = new Intent(this, MainActivity.class);
                //i.putExtra(key_1, userName.getText().toString()); //get id for training and display details in full
                startActivity(i);

            }
}

Error:

01-18 17:01:42.331 3781-3781/com.example.cianm.fyp2018v2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.cianm.fyp2018v2, PID: 3781
                                                                           java.lang.NullPointerException
                                                                               at com.example.cianm.fyp2018v2.RegisterUser$1.onClick(RegisterUser.java:142)
                                                                               at android.view.View.performClick(View.java:4463)
                                                                               at android.view.View$PerformClick.run(View.java:18789)
                                                                               at android.os.Handler.handleCallback(Handler.java:808)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                               at android.os.Looper.loop(Looper.java:193)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5299)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
                                                                               at dalvik.system.NativeStart.main(Native Method)

Any help would be greatly appreciated.

  • Possible duplicate of [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) – Zoe Jan 18 '18 at 17:06
  • What's at RegisterUser line 142, as suggested by your stack trace? – Doug Stevenson Jan 18 '18 at 17:06
  • mFirebaseDatabase.child(fbUser.getUid()).setValue(user); @DougStevenson – Cian McGrane Jan 18 '18 at 17:08
  • 1
    `createUserWithEmailAndPassword()` is an asynchronous operation. You can't call `getCurrentUser()` in the following statement and expect a valid result. – Bob Snyder Jan 18 '18 at 17:15
  • @BobSnyder Ok thanks Bob, do you know where to call in then in order to achieve what was done in the linked question? [link](https://stackoverflow.com/questions/40000899/how-to-link-between-authenticated-users-and-database-in-firebase) – Cian McGrane Jan 18 '18 at 17:23
  • @CianMcGrane Use AuthStateListener https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener – UdeshUK Jan 18 '18 at 17:39
  • @UdeshUk Thank you, using the AuthStateListener did the trick. Posting solution below. – Cian McGrane Jan 19 '18 at 17:28

2 Answers2

0

By using the onAuthStateChanged I was able to get the current users UID and use it as the child node for the User object.

Code:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.cianm.testauth.Entity.User;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterUser extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private DatabaseReference mFirebaseDatabase;

    private EditText mEmail, mPassword;
    private Button mRegister;
    private RadioButton rManager, rPlayer;
    private ProgressBar mProgressbar;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);

        mEmail = (EditText) findViewById(R.id.emailField);
        mPassword = (EditText) findViewById(R.id.passwordField);
        rManager = (RadioButton) findViewById(R.id.radioManager);
        rPlayer = (RadioButton) findViewById(R.id.radioPlayer);
        mRegister = (Button) findViewById(R.id.registerBtn);
        mProgressbar = (ProgressBar) findViewById(R.id.progressBar);

        mAuth = FirebaseAuth.getInstance();

        if (mAuth.getCurrentUser() != null) {
            startActivity(new Intent(RegisterUser.this, HomePage.class));
        }

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser fbUser = firebaseAuth.getCurrentUser();
                if (fbUser != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in" + fbUser.getUid());
                    mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
                    mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
                    Toast.makeText(getApplicationContext(), "Sucessfully signed in with: " + fbUser.getEmail(), Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(RegisterUser.this, HomePage.class));
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }
            }
        };

        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = mEmail.getText().toString();
                String password = mPassword.getText().toString();
                if (TextUtils.isEmpty(email)) {
                    Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (password.length() < 6) {
                    Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!rManager.isChecked() && !rPlayer.isChecked()) {
                    Toast.makeText(RegisterUser.this, "You must select either player or manager", Toast.LENGTH_LONG).show();
                }

                mProgressbar.setVisibility(View.VISIBLE);
                String type = "";
                //radio buttons
                if (rManager.isChecked()) {
                    type = "Manager";
                } else if (rPlayer.isChecked()) {
                    type = "Player";
                }
                mProgressbar.setVisibility(View.VISIBLE);
                mAuth.createUserWithEmailAndPassword(email, password);
                user = new User(email, password, type);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mProgressbar.setVisibility(View.GONE);
    }
}
0

After creating user with email and passwoed you need to sign in the user using mAuth.signInWithEmailAndPassword I think your getcurrentuser.getuid() is returning null and thus the null point exception

And also you might wanna

Modify this part-

          mFirebaseDatabase = FirebaseDatabase.getInstance().getReference("User");
            String userId = mFirebaseDatabase.push().getKey();
            User user = new User(email, password, type);
            mAuth.createUserWithEmailAndPassword(email, password);
            fbUser = FirebaseAuth.getInstance().getCurrentUser();
            mFirebaseDatabase.child(fbUser.getUid()).setValue(user);
            mProgressbar.setVisibility(View.GONE);
            startActivity(new Intent(RegisterUser.this, HomePage.class));

as this

         mAuth.createUserWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                    @Override
                    public void onSuccess(AuthResult authResult) {
                         mAuth.signInWithEmailAndPassword(email,password)
                        fbUser =FirebaseAuth.getInstance().getCurrentUser();
                        String userId = fbUser.getUid();       
                        mFirebaseDatabase=       
    FirebaseDatabase.getInstance().getReference("User").child(userId);
            mFirebaseDatabase.child("<Key>").setValue("<Value>"); 
            // Go on adding as many user details you want to

           mProgressbar.setVisibility(View.GONE);
           startActivity(new Intent(RegisterUser.this, HomePage.class));  
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                      // Do something optional
                });  

Advantage of this approach- This way while retrieving you can simply Look User Node with Child Uid to get the logged in user's detail (without for looping)