pangs
pangs

Reputation: 49

'android.text.Editable android.widget.EditText.getText()' on a null object reference with alert dialog on top of my code

Why am I getting this error? please help. Thank you in advance!

final Button mShowDialog =(Button)findViewById(R.id.btnShowDialog);
          mShowDialog.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  AlertDialog.Builder mbuilder = new AlertDialog.Builder(LoginActivity.this);
                  View mView = getLayoutInflater().inflate(R.layout.dialog_login,null);
                   final EditText hehe=  findViewById(R.id.etUsername);
                  final EditText hehe1=  findViewById(R.id.etPassword);
                  final Button bLogin =  mView.findViewById(R.id.bSignIn);

                 bLogin.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          final String Username = hehe.getText().toString();
                           final String Password = hehe1.getText().toString();

                          // Response received from the server
                          Response.Listener<String> responseListener = new Response.Listener<String>() {
                              @Override
                              public void onResponse(String response) {
                                  try {
                                      JSONObject jsonResponse = new JSONObject(response);
                                      boolean success = jsonResponse.getBoolean("success");

                                      if (success) {
                                          Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                          LoginActivity.this.startActivity(intent);
                                      } else {
                                          AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                          builder.setMessage("Login Failed")
                                                  .setNegativeButton("Retry", null)
                                                  .create()
                                                  .show();
                                      }

                                  } catch (JSONException e) {
                                      e.printStackTrace();
                                  }
                              }
                          };

                          LoginRequest loginRequest = new LoginRequest(Username, Password, responseListener);
                          RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                          queue.add(loginRequest);
                      }
                  });
                  mbuilder.setView(mView);
                  AlertDialog dialog = mbuilder.create();
                  dialog.show();

              }
          });

Upvotes: 0

Views: 157

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

use

final EditText hehe  =  mView.findViewById(R.id.etUsername);
//                      ^^^^^
final EditText hehe1 =  mView.findViewById(R.id.etPassword);
final Button bLogin  =  mView.findViewById(R.id.bSignIn);

because findViewById alone will try to find the view from current layout of activity so you need to find views from mView object which is inflated from dialog_login.xml

Upvotes: 1

Related Questions