-6

Attempt to invoke virtual method com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference :

MyAccount.java

public class MyAccount extends AppCompatActivity  {

    private FirebaseAuth mAuth;
    private TextView email,passwordtxt;
    private FirebaseFirestore mFirestore;
    private String UserId;
    private FirebaseAuth.AuthStateListener mAuthListener;

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

        email       = (TextView)findViewById(R.id.email_txt);
        UserId      = mAuth.getCurrentUser().getUid();
        passwordtxt = (TextView)findViewById(R.id.pass_txt);
        mAuth       = FirebaseAuth.getInstance();
        mFirestore  = FirebaseFirestore.getInstance();

        mFirestore.collection("Hospitals")
                  .document(UserId).get()
                  .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                String user_name = documentSnapshot.getString("email");
                String password  = documentSnapshot.getString("password");

                email.setText(user_name);
                passwordtxt.setText(password);
            }
        });
    }
}
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
Sharma Blog
  • 1
  • 1
  • 5

1 Answers1

1

Change your onCreate method code to :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);

    email = (TextView)findViewById(R.id.email_txt);
    mAuth = FirebaseAuth.getInstance();
    UserId = mAuth.getCurrentUser().getUid();
    passwordtxt = (TextView)findViewById(R.id.pass_txt);

    mFirestore = FirebaseFirestore.getInstance();
   mFirestore.collection("Hospitals").document(UserId).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            String user_name = documentSnapshot.getString("email");
            String password  = documentSnapshot.getString("password");


            email.setText(user_name);
            passwordtxt.setText(password);
        }
    });    
}
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Aj 27
  • 2,316
  • 21
  • 29