-1

So,I am trying to create a login page for server side my application.It's supposed to login only those users who have "IsStaff"="true"....I have used to many if-else statements in this code(for all the possible inputs-like IsStaff=false,or just wrong password in general),and all of them work well(I can say so as they do display their related toast message)...But when I do try to login from account with IsStaff=True....My application crashes with this in my logcat-

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at com.example.admincharade.SignIn$2.onDataChange(SignIn.java:66)
    at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
    at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
    at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

So here's the code of login,and also the User class that I am referring to in this login method.Please help me out.Thanks in advance. Also I am sorry if I didn't follow any formatting rule by chance,I am pretty new and it's honestly pretty overwhelming with so many formatting options.

private void signInUser(final String phone, String password) {
    final ProgressDialog mDialog=new ProgressDialog(SignIn.this);
    mDialog.setMessage("Please Wait");
    mDialog.show();
    final String localPhone=phone;
    final String localPassword=password;
    users.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.child(localPhone).exists())
            {
                mDialog.dismiss();
                User user=dataSnapshot.child(localPhone).getValue(User.class);
                user.setPhone(localPhone);
                if (Boolean.parseBoolean(user.getIsStaff()))
                {
                    if(user.getPassword().equals(localPassword))
                    {
                        Intent login=new Intent(SignIn.this,Home.class);
                        Common.currentUser=user;
                        startActivity(login);
                        finish();
                    }
                    else
                        Toast.makeText(SignIn.this, "Invalid Login", Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(SignIn.this, "Only for Admin", Toast.LENGTH_SHORT).show();
            }
            else
            {
                mDialog.dismiss();
                Toast.makeText(SignIn.this, "No such user found", Toast.LENGTH_SHORT).show();
            }
        }

public class User {
private String Name,Password,Phone,IsStaff;

public User(String name, String password) {
    Name = name;
    Password = password;
}

And this is user

public User() {
}

public String getName() {
    return Name;
}

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

public String getPassword() {
    return Password;
}

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

public String getPhone() {
    return Phone;
}

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

public String getIsStaff() {
    return IsStaff;
}

public void setIsStaff(String isStaff) {
    IsStaff = isStaff;
}
}
Boken
  • 4,825
  • 10
  • 32
  • 42

1 Answers1

-1

You first need to validate both the password (one from user and localPassword) because in current case user.getPassword() is null.

private void isValidString(String text){
   return text != null && text.length() > 0;
}

use the above function to validate both the password and then check whether they are equal or not.

Himanshu Arora
  • 309
  • 4
  • 13
  • I am sorry bro,I don't understand what you are saying exactly...I am pretty new at android programming.Can you tell me what line I need to add in my code and where do I add them? – Prathamesh Choukekar Mar 24 '19 at 07:57
  • Okay no problem, First you've taken the flag isStaff as String and then parse it to Boolean (that's not the right approach you can simply store it as Boolean). Second, In java when you are using Wrapper classes(Integer, Long, String, Boolean etc) you first need to validate whether those values will not be null and then use the particular value. Here In your case the user.getPassword() is null, so you need to validate the String (user.getPassword() and localPassword). – Himanshu Arora Mar 24 '19 at 08:06
  • change the line `if(user.getPassword().equals(localPassword))` to this `if(isValidString(user.getPassword) && isValidString(localPassword) && user.getPassword().equals(localPassword))` – Himanshu Arora Mar 24 '19 at 08:08
  • So I replaced that line with if(isValidString(user.getPassword()) && isValidString(localPassword) && user.getPassword().equals(localPassword)).... It prompted to make new method "isValidString",and so I did- private boolean isValidString(String password) { return password != null && password.length() > 0; } – Prathamesh Choukekar Mar 24 '19 at 08:41
  • But now,when I try to login,it shows toast for IsStaff="False" – Prathamesh Choukekar Mar 24 '19 at 08:42