Skip to content Skip to sidebar Skip to footer

Empty Edit Text Gives Me Error For 0 Value

I'm developing a project and I have problem with my addition for empty EditText( unused) . If I enter a number in my editText then click the button it will show the output in the v

Solution 1:

If you enter nothing you get a blank in text2.getText().toString() and you get a NumberFormatException.

You should set `valueText2 to 0 if no text is entered:

int valueText2 = 0;

if (text2.getText() != null && !text2.getText().toString().equals("")){
   int valueText2 = Integer.parseInt(text2.getText().toString());

Solution 2:

int valueText2 = Integer.parseInt(text2.getText().toString());

If the string is empty, parseInt will throw an exception. You need to check if its empty first and not vall parseInt.

Also, in the future always post the stack trace from logcat for a crash. It tells you exactly what the error is.

Post a Comment for "Empty Edit Text Gives Me Error For 0 Value"