Skip to content Skip to sidebar Skip to footer

How To Stop The App Crashing When String Is Empty

The app crash when there is no text ! I don't have what 'if' to put to fix that ! Thanks you enter TextView TotalTextView; EditText EnterPercentage; EditText EnterPrice; @Overri

Solution 1:

You need to check (obviously) your string. The if that your code is missing is :

if (TextUtils.isEmpty(EnterPercentage.getText()) || TextUtils.isEmpty(EnterPrice.getText())) {
// YOUR CODE
}

Good luck with android development. Questions was already posted


Solution 2:

You surely got a NullPointerException. That's because the text is null and you put a .toString() after.

Try this:

float percenteage = 0;
float prix = 0;

if (EnterPercentage.getText() != null)
    percenteage = Float.parseFloat(EnterPercentage.getText().toString());
if (EnterPrice.getText() != null)
    prix = Float.parseFloat(EnterPercentage.getText().toString());

Solution 3:

you need to validate before trying to convert to float

float percentage = getFloat();
private float getFloat(){
    float fRet = 0.0f;
    if(your validation here){
        fRet = Float.parseFloat(EnterPercentage.getText().toString());;
    }
    return fRet;
}

Post a Comment for "How To Stop The App Crashing When String Is Empty"