android number format exception

前端 未结 1 1948
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 10:48

I get the following exception

java.lang.NumberFormatException: Invalid int: \"\"

It is happening when you try to store a value into

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-26 11:33

    Generally speaking, exception processing is a good approach for this kind of undesired behavior :

    if( obj != null && obj.getTargetWeight() != null ) {
     try {
      int i = Integer.parseInt( obj.getTargetWeight() );
      //do something if target weight is a number
     } catch( NumberFormatException ex ) {
       //do something if target weight was not a number.
     }
    }
    

    ---edited to precise exception type

    0 讨论(0)
提交回复
热议问题