0

I have an alertDialogue popup when a user wants to create a game, and it asks the user how many points they would like to gamble in the game, but it keeps throwing a null reference error and I am not too sure why.

This is my alertDialogue positive button click listener

 alertDialog.setPositiveButton("Confirm Wager", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int whichButton) {
                            createLobbyGame();
                            double wagerD;
                            String wager;
                            TextView wagerRV = findViewById(R.id.wagerRV);

                            wagerD = Double.parseDouble(edittext.getText().toString());
                            wager = Double.toString(wagerD);
                            boolean wage = wager.endsWith("0");

                            if(wage) {
                                wagerRV.setText(wager+"0");
                            } else {
                                wagerRV.setText(wager);
                            }

                        }
                    });

It throws an error when it tries to setText. This is the error it throws

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I know I had this working in the past, but I must have changed something to make it not work properly anymore but I have no idea what I would have changed.

I know this is a very common and simple problem, but I have looked at many other answers and have not found a solution that works for me.

Any help?


TextView declaration:

TextView wagerRV = (TextView) ((AlertDialog.Builder) alertDialog).findViewById(R.id.wagerRV);

How I am defining alertDialog:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(FlipCoinLobby.this);
final EditText edittext = new EditText(FlipCoinLobby.this);

alertDialog.setView(edittext);
brent
  • 131
  • 12
  • As far as I remember, you need to change `findViewById(R.id.wagerRV)` to `alertDialog.findViewById(R.id.wagerRV)`. You might need to cast it as well. – Kartik Nov 05 '18 at 00:16
  • @brentB You probably call `findViewById(R.id.wagerRV)` on a wrong activity: it can't find the `TextView`. Could you post your activity code and layout XML? – Aaron Nov 05 '18 at 00:30

1 Answers1

1

Your wagerRV is null because it can't find R.id.wagerRV.

You need to retrieve views from within onClick() using the dialog reference.

Change

TextView wagerRV = findViewById(R.id.wagerRV);

to

TextView wagerRV = (TextView) ((AlertDialog) alertDialog).findViewById(R.id.wagerRV);

Remove any unnecessary casting (I don't have IDE at the moment).

Update based on comments and question edit:-

alertDialog.setView(edittext) --> your alertDialog does not have any TextView with id R.id.wagerRV. Please check out some examples online on setting the content view with XML and that XML should have that TextView. If your wagerRV is in the activity and not inside the dialog, then declare it at the activity level, not inside onClick of alertDialog.

Update 2

You need to change your builder to the actual AlertDialog using AlertDialog alertDialog = alertDialogBuilder.create();. And then the casting will work too.

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • If I use alertDialog before the findViewById it is unable to resolve findViewById. Is there something I am doing wrong? I believe I have the casting correct. I changed AlertDialog to AlertDialog.Builder because that is what I am using. – brent Nov 05 '18 at 00:27
  • @brentB can you show your exact code of this line and also the way you declared `alertDialog`? AlertDialog extends Dialog, which does have the [findViewById](https://developer.android.com/reference/android/app/Dialog#findViewById(int)) method – Kartik Nov 05 '18 at 00:31
  • Just added it to the bottom of the post – brent Nov 05 '18 at 00:34
  • @brentB added another update, looks like there are many issues with this, I suggest going through a alert dialog tutorial online – Kartik Nov 05 '18 at 00:48
  • Thanks for all the details. I will rework my alertDialog just so it works a bit better, but I solved my issue. The wagerRV was being drawn on the screen after the setText was being called, so I had to swap that around so that after the TextView was drawn it could be set. Thanks for the help. – brent Nov 05 '18 at 00:54