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