0

I'm a beginner developer for Android, and I'm following a tutorial in which there's an app calculating the BMI of the user. However, the app crashes if the user presses the "calculate" button without entering anything in the weight and height fields.

I thought I could solve this by just checking if the weight or height field is null, and if it's true then display a message. However, Android Studio tells me that "the condition height == null || weight == null is always false", and the app still crashes.

Here's the part of the code for the calculate button:

private OnClickListener calculateListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
            String height = height.getText().toString();
            String weight = weight.getText().toString();
            float hValue = Float.valueOf(h);

            if (height == null || weight == null) {
                result.setText("Please enter your weight and your height");
            } else {

                    //calculate BMI
                    float wValue = Float.valueOf(w);
                    if(group.getCheckedRadioButtonId() == R.id.radio2)
                    hValue = hValue / 100;

                    hValue = (float)Math.pow(hValue, 2);
                    float bmi = wValue / hValue;
                    result.setText("Your BMI is " + String.valueOf(bmi));
            }
    }
};

Why is it telling me that the condition is always false? If there's nothing in the fields, is the value not null?

Zezombye
  • 321
  • 4
  • 16
  • 3
    Also add `height.getText().length()==0` – ρяσѕρєя K Jan 19 '16 at 06:49
  • your issue is because you are checking null but in your case it will be empty `""` so i would suggest you to check http://stackoverflow.com/questions/3598770/java-check-whether-a-string-is-not-null-and-not-empty .. change condition to `if (TextUtils.isEmpty(height)|| TextUtils.isEmpty(weight)) {}` – user1140237 Jan 19 '16 at 06:50
  • need more code how you finding view in layout, dont shadow variable of now make Edittext name as heightEt and weightEt – silentsudo Jan 19 '16 at 06:52
  • add the trim() field because when u enter the spaces it will also execute the code to avoid it it add trim function – Muhammad Younas Jan 19 '16 at 06:52

4 Answers4

2

If there's nothing in the fields, is the value not null?

If there's nothing in the fields, the value will be "", which is not null.

So:

if (height.length() == 0 || weight.length() == 0) {

But you also need to guard against the possibility that the user has entered something, but that it cannot be converted to a Float, by catching a NumberFormatException. (I'd also probably use Float.parseFloat to you get a float rather than Float.valueOf which gives you a Float.)

Also, unless I'm very much mistaken, the code as shown won't compile. You have:

String height = height.getText().toString();

Even if you have a height instance field, that's going to fail with

error: variable height might not have been initialized

...and/or

error: cannot find symbol

...because height will resolve to the local, which is a String, and doesn't have getText(). To use the instance field, you'll need YourClassName.this:

String height = YourClassName.this.height.getText().toString();

...or better yet, just use a different local variable name.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • #T.J. Crowder Sir , Off topic , why showing `community wiki` ? – IntelliJ Amiya Jan 19 '16 at 06:54
  • 1
    @IntelliJAmiya: I do that sometimes when I think an answer is so trivial that just about anyone in the community could and would have posted it. – T.J. Crowder Jan 19 '16 at 06:56
  • I thought "" was equal to null, TIL it isn't. Anyway your method worked, thanks :) It still crashed at first but it turned out it was because of the float hValue that couldn't be initialized. Also I just cropped out the part that initializes the height variable. – Zezombye Jan 19 '16 at 07:20
  • @Zezombye: No, `null` is an empty object reference; literally, the variable doesn't refer to any object. In your case, the variable refers to an object (a string), and that string is blank (has no characters in it). Very big difference there. :-) – T.J. Crowder Jan 19 '16 at 07:55
2

Change Your Code with Following

if (height.trim().equals("") || weight.trim().equals("")) {
                result.setText("Please enter your weight and your height");
            } else {

                    //calculate BMI
                    float wValue = Float.valueOf(w);
                    if(group.getCheckedRadioButtonId() == R.id.radio2)
                    hValue = hValue / 100;

                    hValue = (float)Math.pow(hValue, 2);
                    float bmi = wValue / hValue;
                    result.setText("Your BMI is " + String.valueOf(bmi));
            }
Muhammad Younas
  • 1,543
  • 1
  • 22
  • 32
1

This is because, you are already using the fields before your checks like:

String heightStr = height.getText().toString();//appened str
                   ^^^^^^
String weightStr = weight.getText().toString();
                   ^^^^^^

Which means your height and weight can't be null in your if conditions. In this case, you need to check for height and weight (if at all they can be null) before using getText method on them. And later if you need you can check heightStr being holding an empty String.

SMA
  • 36,381
  • 8
  • 49
  • 73
0
   if(height == null && height.isEmpty()) ||(weight == null && weight .isEmpty()))
        {
       result.setText("Please enter your weight and your height");
        }
   else {

                //calculate BMI
                float wValue = Float.valueOf(w);
                if(group.getCheckedRadioButtonId() == R.id.radio2)
                hValue = hValue / 100;

                hValue = (float)Math.pow(hValue, 2);
                float bmi = wValue / hValue;
                result.setText("Your BMI is " + String.valueOf(bmi));
        }
sasikumar
  • 12,540
  • 3
  • 28
  • 48