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?