0

I am trying to access an EditText String in a dialog but I get a null object reference error and I cannot understand why:

        txtLostCreds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create dialog to set new password
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle(R.string.txtResetPassword);
                builder.setView(R.layout.customresetpassworddialog);
                final AlertDialog alert = builder.create();
                builder.setPositiveButton(R.string.txtResetPassword, new DialogInterface.OnClickListener() {
                    final EditText resetNewPassword = alert.findViewById(R.id.resetNewPassword);
                    final EditText resetNewPasswordConfirm = alert.findViewById(R.id.resetNewPasswordConfirm);
                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        assert resetNewPassword != null;
                        assert resetNewPasswordConfirm != null;
                        if (checkPasswords(resetNewPassword, resetNewPasswordConfirm)) {
                           // reset the password
                           RequestParams params = new RequestParams();
                           params.put("uuid", prefs.getString("myUUID", ""));
                           params.put("newpassword", resetNewPassword.getText().toString());
                           AsyncHttpRequest.get("lost_credentials", params, new TextHttpResponseHandler() {
                               @Override
                               public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                   t.MyToast(getResources().getString(R.string.txtConnError), 5000);
                               }

                               @Override
                               public void onSuccess(int statusCode, Header[] headers, String responseString) {
                                   if (responseString.equals("OK")) {
                                       // All done. Close dialog
                                       alert.cancel();
                                   } else {
                                       t.MyToast(getResources().getString(R.string.txtConnError) + responseString, 5000);
                                   }
                               }
                           });
                       }
                   }
                });
                builder.show();
            }
        });

The error happens when I call the function checkPasswords(resetNewPassword, resetNewPasswordConfirm). I have also tried to do a findViewByID on View v but it gives the same error.

The function is as simple as:

private boolean checkPasswords(EditText resetNewPassword, EditText resetNewPasswordConfirm) {
        if (!resetNewPassword.getText().toString().equals(resetNewPasswordConfirm.getText().toString())) {
            resetNewPassword.setError(getResources().getString(R.string.txtDifferentPasswords));
            resetNewPasswordConfirm.setError(getResources().getString(R.string.txtDifferentPasswords));
            return false;
        } else {
            return true;
        }
    }
Fabrizio Mazzoni
  • 1,831
  • 2
  • 24
  • 46

1 Answers1

1

Try to keep a reference to your dialog's view, or at least the parts that you with to control outside of scope in which you created the dialog.

A better practice would be creating a DialogFragment, that is a great way to have more control over your dialog. That way is very useful when there are many operations managed by your dialog.

For example: trying to get input from your Dialog and passing it to your Activity when you click a "save" button. You can use onActivityResult() to send the data to your activity.

Ahmad Sattout
  • 2,248
  • 1
  • 19
  • 42