-1

I have been trying to create this custom dialog window for my app, where it will pop up when pressed and request the user's numerical input. I have tested this custom dialog thing before and it has worked in the past successfully, however when I tried it in this case, it kept throwing out the error of "Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference" at

final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
numberOfPoints = Integer.parseInt(inputNumber.getText().toString()); 

This error, when pressing the button to bring up the custom pop-up window, would cause the screen to blackout for a few seconds before returning to the app's main menu. What's supposed to happen is the dialog window popping up to get the user's input

private void openDialog() {
        input_score = new Dialog(this);
        beginButton = findViewById(R.id.beginButton);
        final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
        numberOfPoints = Integer.parseInt(inputNumber.getText().toString());

        input_score.setContentView(R.layout.input_score);
        input_score.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        input_score.show();

        beginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inputNumber.getText().toString().trim().isEmpty()) {
                    //change textview to error for a few seconds before returning to normal
                } else {
                    if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
                        startActivity(new Intent(MainMenu.this, MainGame.class));
                    }
                }
            }
        });

Full Error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.rockpaperscissors, PID: 4683
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.rockpaperscissors.MainMenu.openDialog(MainMenu.java:67)
        at com.example.rockpaperscissors.MainMenu.access$000(MainMenu.java:20)
        at com.example.rockpaperscissors.MainMenu$1.onClick(MainMenu.java:47)
        at android.view.View.performClick(View.java:7217)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7191)
        at android.view.View.access$3500(View.java:828)
        at android.view.View$PerformClick.run(View.java:27679)
        at android.os.Handler.handleCallback(Handler.java:900)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8347)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)

I'm not sure how I go about exactly fixing this, so please do point out the errors made in the code to improve and sort it out. Thank you

EDIT: As @Shay Kin showed, he used a onShowListener then put the button Listener within it, so that it would know that when this dialog would pop up, then to pop up the button function along with it. In my case, the reason the code kept breaking was not only because of mispositioning the .show but also due to the fact that I had this variable "numberOfPoints" which kept parsing an empty text field and breaking the code since no value was inside. I had not noticed this until I went through it deeply again and deleted that variable.

The things I did to fix the code was by implementing his solution, which had already incorporated the buttonListener into it, and delete the numberOfPoints variable. This resulted in the program being fixed and working successfully

Thanos
  • 386
  • 2
  • 13

3 Answers3

1

Maybe because the EditText is in a different scope? Try declaring the EditText outside the function (Beside your other components). The onClick() function is a callback so maybe the EditText is being disposed of once the openDialog() finishes executing.

thisiszammy
  • 61
  • 1
  • 6
1

Before you call Dialog.show(), the view doesn't yet exist, thus your findViewById() doesn't find the desired view, returning null instead. The views would only be available after calling either show() or create().

Since you don't actually need it in advance, you can move this initialization to where you actually need it.

beginButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Initialize it here
        final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
        if (inputNumber.getText().toString().trim().isEmpty()) {
            //change textview to error for a few seconds before returning to normal
        } else {
            if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
                startActivity(new Intent(MainMenu.this, MainGame.class));
            }
        }
    }
});

However, it's important to mention that you should avoid using the Dialog class directly, as per official documentation.

0

As @Gabriel said You got NullPointerException because you initialize the EditText After the dialog is shown so you can add listener and when you're Dialog is become visible here you can initialize all your view

private void openDialog() {
    input_score = new Dialog(this);
    input_score.setContentView(R.layout.speach_dialog);

    input_score.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    
    input_score.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            beginButton = findViewById(R.id.beginButton);
            final EditText inputNumber = input_score.findViewById(R.id.inputNumber);
            numberOfPoints = Integer.parseInt(inputNumber.getText().toString());
            beginButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (inputNumber.getText().toString().trim().isEmpty()) {
                        //change textview to error for a few seconds before returning to normal
                    } else {
                        if (Integer.parseInt(inputNumber.getText().toString()) <= 0 || Integer.parseInt(inputNumber.getText().toString()) > 900) {
                            startActivity(new Intent(MainMenu.this, MainGame.class));
                        }
                    }
                }
            });
        }
    });

    input_score.show();

}
Shay Kin
  • 2,539
  • 3
  • 15
  • 22
  • Ohh I see that, so before then I was just referencing to nothing hence the error, I did not know that I had to initialize after creation since I thought it would not do anything, but I see now. Regardless, thank you for the help from you, Gabriel and thisiszammy. – Thanos Mar 12 '21 at 17:59
  • Though I would like to point out something, when using the solution, there's another issue that comes into place being the fact that pressing beginButton does not actually move to the MainGame class as desired. The conditions do not work either as they do not release the error when the number is greater than 900 or empty. https://gyazo.com/6f877e131770205791eecdff03707015 – Thanos Mar 12 '21 at 18:02
  • ah yeah the begin Button does not belong to the dialog, I made a mistake so you declared it in your activity – Shay Kin Mar 12 '21 at 18:08
  • Oh no no the button does belong to the dialog, in fact, it appears with the dialog itself when pressing the button to pop up the dialog window within game, just that I'm not sure why it does not work. I did take your advice of it not belonging to the dialog by moving the button listener out of it(but still within the "openDialog" function) but I ended up getting the same error as before. I am somewhat new to this so I'm just learning from these mistakes – Thanos Mar 12 '21 at 18:18
  • if is inside the dialog use `beginButton = input_score.findViewById(R.id.beginButton);` instead of `beginButton = findViewById(R.id.beginButton);` – Shay Kin Mar 12 '21 at 18:22
  • For some reason that still does not work, I tested it out by removing the conditional statement and just making it instead go to the maingame class when the button was pressed with another fail. The issue comes from the button listener itself for some reason – Thanos Mar 12 '21 at 18:43
  • can you please tel me what error you have now ? – Shay Kin Mar 12 '21 at 18:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229839/discussion-between-thanos-and-shay-kin). – Thanos Mar 12 '21 at 18:57