agr
agr

Reputation: 127

Not able to figure out issue in the Password checker API

 public class HomePage extends AppCompatActivity{

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_home_page);
         rbtn=this.findViewById(R.id.rbtn);
         v=this.getCurrentFocus();



    rbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            callLoginDialog();
        }
    });

}
private void callLoginDialog()
{
    myDialog = new Dialog(this);
    myDialog.setContentView(R.layout.register);
    myDialog.setCancelable(true);
    regbtn = (Button) myDialog.findViewById(R.id.rsubmit);

    rmail = (EditText) myDialog.findViewById(R.id.remail);
    rpass = (EditText) myDialog.findViewById(R.id.rpass);
    rcpass=myDialog.findViewById(R.id.rcpass);
    rname=myDialog.findViewById(R.id.rname);
    rphone=myDialog.findViewById(R.id.rphone);
    myDialog.show();
    pB = (ProgressBar) findViewById(R.id.progressBar);
    sv = (TextView) findViewById(R.id.password_strength);

    pchecker=new PasswordChecker(this,v,pB,sv);
    rpass.addTextChangedListener(pchecker);
  }

PasswordChecker class

 public class PasswordChecker implements TextWatcher {

    Activity a;
    View v;
    String txt;
    ProgressBar progressBar;
    TextView strengthView;
    public PasswordChecker(Activity a,View v,ProgressBar p,TextView tv) {
        progressBar=p;
        strengthView=tv;
        this.a = a;
        this.v=v;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        Toast.makeText(a, ""+s, Toast.LENGTH_SHORT).show();
        updatePasswordStrengthView(s.toString());//If i comment this line,the app doesn't crash
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }


    private void updatePasswordStrengthView(String password) {


        if (TextView.VISIBLE != strengthView.getVisibility())
            return;

        if (password.isEmpty()) {
            strengthView.setText("");
            progressBar.setProgress(0);
            return;
        }
        PasswordStrength str = PasswordStrength.calculateStrength(password);
        txt=str.getText(a).toString();
        strengthView.setText(txt);
        strengthView.setTextColor(str.getColor());

        progressBar.getProgressDrawable().setColorFilter(str.getColor(), android.graphics.PorterDuff.Mode.SRC_IN);
        if (txt.equals("Weak")) {
            progressBar.setProgress(25);
        } else if (txt.equals("Medium")) {
            progressBar.setProgress(50);
        } else if (txt.equals("Strong")) {
            progressBar.setProgress(75);
        } else {
            progressBar.setProgress(100);
        }
    }
}

I have used the following API,https://github.com/yesterselga/password-strength-checker-android

This API is used to check the password strength.The application crashes when i try to type a password.This error doesn't occur whenever i comment the updatePasswordStrengthView method in PasswordChecker class.So I know that there is some mistake in the updatePasswordStrengthView method.Please help me out.

The following is the logcat of the error:

07-31 21:06:23.280 19825-19825/akshay.shoppingapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: akshay.shoppingapplication, PID: 19825
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at akshay.shoppingapplication.PasswordChecker.updatePasswordStrengthView(PasswordChecker.java:59)
        at akshay.shoppingapplication.PasswordChecker.onTextChanged(PasswordChecker.java:37)

Upvotes: 0

Views: 36

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Use this

txt=str.getText(a).toString();

instead of

txt=str.getText(this).toString();

because this mean context which is acquired by an activity(from application) when it is started using an Intent( or as a launcher intent in manifest ) but in your case, it's just acting as a normal class hence no context will exist hence this means nothing as context in your PasswordChecker class

Note: PassWorkChecker should only contain your checking logic hence should not extend an Activity instead use a variable which is representing context, wherever you need Context

Upvotes: 3

Related Questions