-4

I am trying to login with phone number and password but to get the phone number from firebase database through model class is giving a null pointer exception. Please Help

I have already tried to get all the other String variables which works properly except the phone number.

Model Class

public class Users {

    private String Name;
    private String Address;
    private String Phone;
    private String Email;
    private String Password;

    public Users() {
    }

    public Users(String name, String address, String phone, String email, String password) {
        this.Name = name;
        this.Address = address;
        this.Phone = phone;
        this.Email = email;
        this.Password = password;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    public String getAddress() { return Address;  }

    public void setAddress(String address) {
        this.Address = address;
    }

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String phone) {
        this.Phone = phone;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        this.Email = email;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        this.Password = password;
    }

}

Login_Actvity

private void allowAccesstoAccount ( final String phonestring, final String passwordstring){

    final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    database.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if ((dataSnapshot.child(parentName).child(phonestring).exists())) {

                    Users usersData = dataSnapshot.child(parentName).child(phonestring).getValue(Users.class);

                    //`userData.getPhone()` gives warning
                    //while  `userData.getPassword()` works properly 

                    if(usersData.getPhone().equals(phonestring)){
                    if(usersData.getPassword().equals(passwordstring)){
                        Toast.makeText(Login_Activity.this, "Login Successful ", Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();

                        Intent intent = new Intent(getApplicationContext(),Home_Activity.class);
                        startActivity(intent);
                    }
                }
                else {
                    Toast.makeText(Login_Activity.this, "Login Failed ", Toast.LENGTH_SHORT).show();
                    loadingBar.dismiss();
                }
            }
            else {
                Toast.makeText(Login_Activity.this, "Account with this " + phonestring + " phone number does not exist", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();
                Toast.makeText(Login_Activity.this, "Try the correct one", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

java.lang. NullPointerException is the exception

Ashish
  • 6,791
  • 3
  • 26
  • 48

1 Answers1

0

I was also getting same issue. Solution :

Firebase parse data by getter setter method, if it doesn't find right getter then it doesn't parse, So there are 2 solutions for that :

  1. Make Phone to public, so firebase can access it directly :

    //private String Phone;

    public String Phone;

  2. change Phone to camel notation "phone" and make its getter setter :

    @SerializedName("Phone") private String phone;

    public String getPhone() { return phone; }

    public void setPhone(String phone) { this.phone = phone; }

NehaK
  • 2,639
  • 1
  • 15
  • 31