0

I'm trying to create a login and register application with Google Firebase and Android Studio.

The error:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
       at com.example.cmp6213_2.Login$3.onDataChange(Login.java:79)
       at com.google.firebase.database.Query$1.onDataChange(Query.java:191)
       at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
       at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
       at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
       at android.os.Handler.handleCallback(Handler.java:942)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:201)
       at android.os.Looper.loop(Looper.java:288)
       at android.app.ActivityThread.main(ActivityThread.java:7872)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

LoginActivity.java

    public void checkUser(){
        String userEmail = loginUsername.getText().toString().trim();
        String userId = userEmail.substring(0, 4);
        String userPassword = loginPassword.getText().toString().trim();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");
        Query checkUserDatabase = reference.orderByChild("email").equalTo(userEmail);
        checkUserDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.exists()){
                    loginUsername.setError(null);
                    String passwordFromDB = snapshot.child(userId).child("password").getValue(String.class);
                    if (passwordFromDB.equals(userPassword)) {
                        loginUsername.setError(null);
                        Intent intent = new Intent(Login.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        loginPassword.setError("Invalid");
                        loginPassword.requestFocus();
                    }
                } else {
                    loginUsername.setError("User does not exist");
                    loginUsername.requestFocus();
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

I appreciate any help, thank you for your time.

I can't figure out where the issue is coming from and what to do to fix it and I would appreciate anyone who could help me guide me to the culprit.

HSN I
  • 1
  • 2
  • Seems like the error is indicating that your `passwordFromDB.equals(userPassword)` is returning a null. Add a null checker before calling `.equals()`. Like this, `if (passwordFromDB != null && passwordFromDB.equals(userPassword))` This would prevent this error. – Maleesha97 May 16 '23 at 18:24
  • @Maleesha97 Hi thanks for the answer. This did turn out to remove the error but not the problem. The code doesn't retrieve the password data from the database, do you have any idea on why the code isn't pulling any data? Any help would be greatly appreciated, thanks. – HSN I May 16 '23 at 18:56
  • I hope this is a homework question and not for a production project. This code implies that passwords are stored in clear in the DB without any form of hashing. This is very dangerous security wise. If this is destined for production, I highly encourage you to read about password hashing (using bcrypt, pbkdf2, etc...), or external authentication solutions that can handle this sensitive process for you. – LordOfThePigs May 16 '23 at 18:59
  • @HSN I don't think it is a good idea to retrieve the password from database to client side code either. However, your issue is in the way you are fetching from the database. I would suggest you to try and log the snapshot to see what sort of data you are fetching. See if your reference(Path) is correct for the DB. – Maleesha97 May 16 '23 at 20:34
  • @LordOfThePigs and Maleesha97 Hi, you are correct this is not a production project and so it's not an issue for me that the passwords are being stored in this way. Thanks for the advice. I will look into the reference(Path) and see what data it's trying to collect vs. what it should be collecting. – HSN I May 16 '23 at 22:14

1 Answers1

0

The console error says that the variable passwordFromDB is null.

Please check if the variable isn't null.

if (passwordFromDB.equals(userPassword)) {
     loginUsername.setError(null);
     Intent intent = new Intent(Login.this, MainActivity.class);
     startActivity(intent);
    }
 else {
    loginPassword.setError("Invalid");
    loginPassword.requestFocus();
 }

Hope this helpfull to you.

Andy Preston
  • 779
  • 4
  • 9
  • 23
  • Hi, thanks for your answer. This fixes the error I'm getting but it means that the code is not retrieving the password from the database. Do you have any ideas of why that's the case? – HSN I May 16 '23 at 18:54