0

I just ran into a small null pointer problem. I was setting up an EditView (textInputNewWeight) for my Dialog Window. The code you see down here is a button with id - (confirmer), which checks the editView after clicking on it. It should make a toast message that the field is empty but it crashed every time when the editView is empty and I press the confirm button (code below).

Thanks for any help in advance

How should I write the first if statement to make it showing the toast message instead of crashing?

confirmer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (textInputNewWeight == null){
                    Toast.makeText(getContext(), "This field cannot be empty.", Toast.LENGTH_SHORT).show();
                }


            }
        });
Kristian
  • 165
  • 2
  • 16

2 Answers2

0

You should check also Empty Text case.

 confirmer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {      
if (textInputNewWeight == null || TextUtils.isEmpty(textInputNewWeight)){
                    Toast.makeText(getContext(), "The field cannot be empty.", Toast.LENGTH_SHORT).show();
                } else {
                    int weightNumberInput = Integer.parseInt(textInputNewWeight.getEditText().getText().toString().trim());

                    if (weightNumberInput > 0 && weightNumberInput < 100){ //1 - 99
                        confirmInput(1);
                        dismiss();
                    }
                    else if (weightNumberInput > 99 && weightNumberInput < 1000){ //100-999
                        confirmInput(2);
                        dismiss();
                    } else {
                        Toast.makeText(getContext(), "The number was set incorrectly.", Toast.LENGTH_SHORT).show();
                    }

                }


            }
        });
0

A NullPointerException almost always means that the code either:

  • Tried to unbox a null object to a primitive or
  • Tried to call a method on a null object.

The second case is the most common one, and one place to be very careful about it is in long chains of method calls, such as the line

int weightNumberInput = Integer.parseInt(textInputNewWeight.getEditText().getText().toString().trim());

If any of those function calls returns a null then you'll get a NullPointerException.

I'd start by checking what is returned from textInputNewWeight.getEditText() when there's nothing in your box. It's probably null. The quickest way to check is often to put a breakpoint at the start of the function, run the project in debug mode, and inspect the variables in your IDE's debugger.

Player One
  • 607
  • 6
  • 12
  • The second case happens here for sure and its the textInputNewWeight which causes the problem, the editView is empty and later it compares null with this variable which is int. What I dont understand is why it didnt stop at the first null condition which is if (textInputNewWeight == null) when its empty (null). – Kristian Oct 19 '20 at 21:47
  • @Kristian empty and `null` are not the same. Empty means that `textInputNewWeight` is a not-null object, with nothing in it (i.e. the result of `textInputNewWeight.getEditText()` will be null or empty). `null` means that `textInputNewWeight` doesn't point to an object at all. The NPE isn't coming from calling `getEditText()` on `textInputNewWeight`, it's (probably) coming from calling `getText()` on the result of `textInputNewWeight.getEditText()` – Player One Oct 19 '20 at 21:55
  • I think the same that it's coming from `getText()`, however I could cast requireNonNull on it prevent it from happening but.. I would like to make the code in such conditions stop in the first if statement, which is `if(textInputNewWeight == null)`, because it only crashes when its empty. So what should I write instead of the current if statement? – Kristian Oct 19 '20 at 21:59
  • @Kristian Add checks for all the conditions you want to stop there on (in this case adding an `|| textInputNewWeight.getEditText() == null` would be a start, but won't cover all the cases - what if the input is whitespace, or something that isn't a number?). If it's getting big and hard to read, then it's a good idea to make a new function that returns a boolean, and do the checking in that. – Player One Oct 19 '20 at 22:07
  • Well in my xml of that Dialog the `` a.k.a the EditText is set to `android:inputType="number"` so I got the other cases covered :), but this one is different.. no matter what I put into the first if statement, it always crashes and I can't find a solution lol – Kristian Oct 19 '20 at 22:11
  • @Kristian if checking for getEditText == null doesn't sort it thin it could be getEditText().getText() returning null (or something else). The quickest way to find out what would be to put a breakpoint at the start of the function, run it in debug mode and inspect the variables in your IDE's debugger. – Player One Oct 19 '20 at 22:15
  • I tried to run a debugger but I cant see much in it tho, but can you? https://ctrlv.cz/ddU6 – Kristian Oct 19 '20 at 22:29
  • @Kristian `alt+F8` will bring up a box where you can evaluate expressions. Put your full line in there and evaluate it. You'll hopefully get a null pointer exception. Then start at the end and remove function calls and re-evaluate until you find the one that returns `null` – Player One Oct 19 '20 at 22:37