Home » Android » android – FATAL EXCEPTION: Java.lang.NumberFormatException: For input string " "-Exceptionshub

android – FATAL EXCEPTION: Java.lang.NumberFormatException: For input string " "-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


I am facing the FATAL EXCEPTION and it cause my application crash.
Code is here

int datedob=Integer.parseInt(dobdate.getText().toString());

I am still learning Android studio programming, so please help me.
My edittext dobdate is already “Number” type.

How to&Answers:

If you apply good programming practices, you should be able to avoid this issue. First, the Fatal Error is going to crash your program, so, you want to avoid that at all costs. To do that, you should try to ‘catch’ the error. The example below catches the NumberFormatException. If the error occurs, then the ‘catch’ block of code will execute and print the error to the Log file.

However, we should try to prevent the error in the first place by examining the string before trying to convert it to an int. So, first read it into a string variable, then get rid of any spaces, then check that the string is not empty and only then should you try to convert it to an int.

It is also good form to use curly braces around your ‘if’ blocks of code. If you don’t you can only write one instruction, but often, you might want to add more than one instruction, so, always use them, even if just for one instruction. It makes it more visible which code is part of the block.

int datedob = 0;
string sdatedob = dobdate.getText().toString();
sdatedob = sdatedob.replace(" ", "");

if (sdatedob.length() > 0) {

    try {
        datedob = Integer.parseInt(sdatedob);
        if(datedob>31) {
    } catch (NumberFormatException e) {
      Log.e("MyApp", e.getLocalizedMessage());

} else {
    Log.w("MyApp", "dobdate is empty");

The clue to your error was in the exception message in your title: FATAL EXCEPTION: Java.lang.NumberFormatException: For input string “ ”

Your string contained a space in it. A space is not a valid int, so, that is what caused your exception. So, we should remove all spaces before parsing to int. However, since that is all that your string contained, then after removing the space, the string will have a length of zero, also not a valid int and will also cause an exception. So, you want to check for each of these cases before even trying to parse the string into the int. That allows you to handle each case differently, so, you might want to provide a message to the user that their datedob is empty, or that it has non-numeric characters in it, whatever will be helpful. Not sure how your edittext got a space in it, if formatted to be numeric, but the original exception message showed that it did.

As for e.getLocalizedMessage(), that is a method that is part of each different type of Exception, which contains an explanation of the reason for the exception. It is the same as e.getMessage(), except that if there is any date or numeric formatting in the message, the getLocalizedMessage() method will format those for the location that is active on the device, so, it might be a more readable message for your users or for you in debugging logs.