1

I tried to make a login screen for my application, but when I enter the right Id and password that should work my application keeps stopping. Furthermore, When I debug the codes, error messages such as:

 W/ClassMapper: No setter/field for User found on class com.example.(Projectname).User
 No setter/field for BookReport found on class com.example.(Projectname).User

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.(Projectname), PID: 3339
 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at com.example.(Projectname).LoginScreen$1$1.onDataChange(LoginScreen.java:79)

Comes out. Why does it happen?

2 Answers2

1

It seems that the com.example.hellobook.User class does not define setters for its fields. Try defining a setter for all the fields so the statement foundUser = dataSnapshot.getValue(User.class); can create a correct User object

e.g.

public void setBookReport(BookReport bookReport){
    this.bookReport = bookReport;
}
Maher Malaeb
  • 321
  • 2
  • 5
-1

sir your error is this line if (userId.equals(id)) becuase you set the value of dataSnapShot in foundUser Object outside the for loop.

    ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            foundUser = dataSnapshot.getValue(User.class);
                            userId = foundUser.getId();
                            userPw = foundUser.getPw();
                            if (userId.equals(id)) {
                                if (userPw.equals(psw)) {
                                    startActivity(new Intent(LoginScreen.this, LandingScreen.class));
                                }

                            }
                            else {
                                Toast.makeText(LoginScreen.this, "Wrong ID or password. Try again", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }
                    @Override
                    public void onCancelled (DatabaseError error) {
                        @NonNull DatabaseError databaseError;                        }
                });
            };
Hasnain Sabir
  • 200
  • 1
  • 7