Yamiess
Yamiess

Reputation: 251

Firebase NullPointException when logging in

Hello im trying to create a firebase logging in auth with 2 different users, the admin, and user. but like when i was trying to log in, The application would crash. Heres the error i think The error i think?

my databaseHere is my Databbase

and here is my code on the login

public class LoginActivity extends AppCompatActivity {
private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;
private Button btnSignup, btnLogin, btnReset;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);
    auth = FirebaseAuth.getInstance();
    setContentView(R.layout.activity_login);


    inputEmail = (EditText) findViewById(R.id.email);
    inputPassword = (EditText) findViewById(R.id.password);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    btnSignup = (Button) findViewById(R.id.btn_signup);
    btnLogin = (Button) findViewById(R.id.btn_login);
    btnReset = (Button) findViewById(R.id.btn_reset_password);

    //db
    mFirebaseInstance = FirebaseDatabase.getInstance();

    //Get Firebase auth instance
    auth = FirebaseAuth.getInstance();
    btnSignup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, signup.class));
        }
    });
    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
        }
    });
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = inputEmail.getText().toString();
            final String password = inputPassword.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;
            }

            progressBar.setVisibility(View.VISIBLE);
            //authenticate user
            auth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            progressBar.setVisibility(View.GONE);
                            if (!task.isSuccessful()) {
                                // there was an error
                                if (password.length() < 6) {
                                    inputPassword.setError(getString(R.string.minimum_password));
                                } else {
                                    Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
                                }
                            } else {
                                onAuthSuccess(task.getResult().getUser());

                            }
                        }

                        private void onAuthSuccess(FirebaseUser user) {
                            if (user !=null){
                                mFirebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid()).child("type");
                                mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        String value = dataSnapshot.getValue(String.class);
                                        if(Integer.parseInt(value) == 1) {
                                            startActivity(new Intent(LoginActivity.this, UserActivity.class));
                                            Toast.makeText(LoginActivity.this, "Welcome User", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }else {
                                            startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                                            Toast.makeText(LoginActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                                            finish();
                                        }

                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

                            }
                        }
                    });
        }
    });
}

I feel like the problem lies on the ondata change one but I don't know what to do to fix this, so im asking for your help :O

Upvotes: 0

Views: 45

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

Try this:

  mFirebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Accounts").child("Users").child(user.getUid());
                            mFirebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String value = dataSnapshot.child("type").getValue(String.class);
                                    if(Integer.parseInt(value) == 1) {
                                        startActivity(new Intent(LoginActivity.this, UserActivity.class));
                                        Toast.makeText(LoginActivity.this, "Welcome User", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else {
                                        startActivity(new Intent(LoginActivity.this, AdminActivity.class));
                                        Toast.makeText(LoginActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }

                                }

try the above, have the reference at the userid then inside onDataChange retrieve the type. you need to reference in order and specify the child name like this:

String value = dataSnapshot.child("type").getValue(String.class);

Upvotes: 1

Related Questions