Skip to content Skip to sidebar Skip to footer

App Crashes When Textfield Is Empty

I have tried too many things but after pressing calculate(hesapla) button app crashes everytime. For example: if (vize1.getText().toString().equals('') || fina1.getText().toString

Solution 1:

that's because you want to convert empty string to int. here:


int v1 = Integer.parseInt(vize1.getText().toString());
        int f1 = Integer.parseInt(fina1.getText().toString());
        int s1 = Integer.parseInt(sort1.getText().toString());

Do this instead for all like:

int v1 = "".equals(vize1.getText().toString()) ? 0 : Integer.parseInt(vize1.getText().toString());

Solution 2:

vize1.getText().toString().equals("") 

change it to "".equals(vize1.getText().toString())

(apply this to rest of your conditions in if statement)


in your way if vize1 is null for any reason (maybe has not been declared yet) compiler will throw a nullPointException and crash the app.

Solution 3:

Use the isEmpty() method to test the condition. It uses less code and is more clear about your intentions.

Post a Comment for "App Crashes When Textfield Is Empty"