简体   繁体   中英

Android studio “ Attempt to invoke virtual method on a null object reference”

I can't get past this “Attempt to invoke virtual method on a null object reference” error.

So my Button on the code below opens a CustomDialog with an EditText where the user can update his email address.

        btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
        btnChangeEmail.setOnClickListener(new OnClickListener() {
            @Override

            //Change Email Dialog
            public void onClick (View arg0){
                final Dialog dialog = new Dialog(context);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.email_dialog);
                //dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);

                Button dialogButton = (Button) dialog.findViewById(R.id.buttonSave);
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
                        AlertDialog.Builder alert = new AlertDialog.Builder(context);
                        alert.setTitle("Are you sure?");
                        alert.setMessage("Use " + txtNewEmail.getText().toString() + " as your new address?");
                        alert.setCancelable(false);
                        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                dialog.dismiss();
                            }
                        });
                        AlertDialog alertDialog = alert.create();
                        alertDialog.show();
                    }
                });

                Button exitButton = (Button) dialog.findViewById(R.id.buttonExit);
                exitButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
                Window window = dialog.getWindow();
                float h = (float) (getResources().getDisplayMetrics().heightPixels * .9);
                float w = (float) (getResources().getDisplayMetrics().widthPixels);
                int height = (int) h;
                int width = (int) w;
                window.setLayout(width, height);
            }
        }

By clicking the save button in the aforementioned custom dialog, it opens up an alert which prompts the user if he in fact wants to continue and overwrite his registered email address. I clearly instantiated the txtNewEmail but always get the null object reference error on the line where it says

alert.setMessage("Use " + txtNewEmail.getText().toString() + " as your new address?");

Any help is appreciated. Thank you.

Here is my full logcat error...

03-05 01:51:57.704 4512-4512/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.twenty, PID: 4512
                                             java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                 at com.example.twenty.SettingsActivity$4$1.onClick(SettingsActivity.java:193)
                                                 at android.view.View.performClick(View.java:5198)
                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

您在onClickListener内的edittext还将使用对话框来查找此类ID

EditText txtNewEmail = (EditText)dialog.findViewById(R.id.editTextNewEmail);

remove and add initiated txtNewEmail outside of Listener

final EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);
btnChangeEmail=(Button)findViewById(R.id.buttonChangeEmail);
EditText txtNewEmail = (EditText)findViewById(R.id.editTextNewEmail);

this object is null(txtNewEmail) call this method(findViewById) by the view father

change

  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();
                        }
                    });

with

  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            alert.dismiss();
                        }
                    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM