-1

I have auth with firebase when I type a userid that exists in my database then it logs in successfully, but when I type a wrong key which is not in my firebase then the apk crashes.

private DatabaseReference ref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userID = (EditText) findViewById(R.id.Username);
        ref = FirebaseDatabase.getInstance().getReference().child("muhamadgamerstrings");
    }

     public void btnlogin_Click(View view) {
        final String usrID = userID.getText().toString();

         try {
        ref.child(usrID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                muhamadgamerstrings Userr = dataSnapshot.getValue(muhamadgamerstrings.class);
                if(usrID.equals(Userr.getUserID()))
                {
                    Toast.makeText(MainActivity.this,
                            "login Succesful",Toast.LENGTH_SHORT).show();
                    Intent start = new Intent(MainActivity.this,start.class);
                    startActivity(start);
                }else {
                    Toast.makeText(MainActivity.this,
                            "Wrong Key",Toast.LENGTH_SHORT).show();
                }

            }
     @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
        }catch (Exception xe){
            Toast.makeText(MainActivity.this,
                    "login false",Toast.LENGTH_SHORT).show();
        }

    }

}

That is my mainactivity. The error is from logcat:

if(usrID.equals(Userr.getUserID())) java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.yuhamadgamer.loginwithfire.muhamadgamerstrings.getUserID()' on a null object reference

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

The value in the Firebase realtime database muhamadgamerstrings is null.

because

when i type a wrong key wich is not im my firebase then the apk get crash

when you type a id which is not in database, database will send a null snapshot. so that's the reason of NullPointerException.

so you need to check it null before doing operations

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                muhamadgamerstrings Userr = dataSnapshot.getValue(muhamadgamerstrings.class);
                if(Userr != null && usrID.equals(Userr.getUserID()))
                {
                    Toast.makeText(MainActivity.this,
                            "login Succesful",Toast.LENGTH_SHORT).show();
                    Intent start = new Intent(MainActivity.this,start.class);
                    startActivity(start);
                }else {
                    Toast.makeText(MainActivity.this,
                            "Wrong Key",Toast.LENGTH_SHORT).show();
                }

            }
majurageerthan
  • 2,169
  • 3
  • 17
  • 30