Error: NumberFormatException inside an Asynctask: java.lang.NumberFormatException: For input string: ""

1

I convert the whole number that the user enters to hour / minutes / seconds as follows

int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000;
                i.putExtra("tiempo",time);
                startService(i);

And as you see that number I send it to a service and it executes it, the problem is that if I use it within a Asynctask it throws the following error in the logcat:

  

java.lang.NumberFormatException: For input string: ""                                                                                        at java.lang.Integer.parseInt (Integer.java:533)

Inside my asyn I have it in the following way:

 @Override
        protected void onPostExecute(String result) {


            pdLoading.dismiss();

            if(result.equalsIgnoreCase(""))
            {
                int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000; // esta es la linea del problema
                i.putExtra("tiempo",time);
                startService(i);

                Toast.makeText(getApplicationContext(),"Comienza la cuenta.",Toast.LENGTH_SHORT).show();

The funny thing that if I get this function out of async works perfect.

    
asked by Ashley G. 13.11.2017 в 23:03
source

2 answers

1

If you receive this error:

  

java.lang.NumberFormatException: For input string: "" at   java.lang.Integer.parseInt

indicates that you can not perform the conversion to integer type of the value "". In this case you can check the length to know if the EditText has content,

 if(minutos.getText().toString().length()>0 ) {
     //Tiene contenido!
 }else{
     //No tiene contenido.
 }

However, the above may still show an error message if the user enters a non-numeric value.

The best way to validate that the content of EditText is a value to be converted to integer by Integer.parseInt() is to validate that the value is numeric.

For this you can use the following method:

public static boolean isNumeric(String number){
    boolean result = false;
    try{
        if(number != null){
            Double.parseDouble(number);
            result = true;
        }
    }catch(NumberFormatException nfe){
        Log.e(TAG, "NFException value: " + number);
    }
    return result;
}

and use it like this:

if(isNumeric(minutos.getText().toString().trim())) {  //Contiene valor numérico!
     int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000;
     i.putExtra("tiempo", time);
     startService(i);

     Toast.makeText(getApplicationContext(), "Comienza la cuenta.", Toast.LENGTH_SHORT).show();

}else{
   //No contiene valor numérico!
}
    
answered by 14.11.2017 / 00:23
source
1

I solved the problem in the following way:

 if( minutos.getText().toString().length()>0 ) {
                   int time = Integer.parseInt(minutos.getText().toString().trim()) * 60 * 1000;
                   i.putExtra("tiempo", time);
                   startService(i);

                   Toast.makeText(getApplicationContext(), "Comienza la cuenta.", Toast.LENGTH_SHORT).show();
               }

It happened that I did not receive the number of editText therefore I received the exception . It was a simple mistake, but I had overlooked the basics.

    
answered by 13.11.2017 в 23:15