Skip to content
Advertisement

How to resolve Java.lang.numberformatexception: empty string

I have a utility function which convert parseDouble value to string.

public static BigDecimal setValue(Object o) {
  BigDecimal value = new BigDecimal(0);
  if(o!= Null){
    value=BigDecimal.valueOf(Double.parseDouble(o.toString()));
  }
  return value;
}

I have tried with (o!=null && !isEmpty(o)) and (o!="" && o!=null) but it is still throwing same error.

Transaction amount which is processing this utility function contains empty value.

Advertisement

Answer

Firstly I don’t understand why you are taking object type as an input, however to resolve your issue you can do something like this. But I would strongly advice you to change the method signature it is misleading.

public static BigDecimal setValue(Object o) {
    var value = new BigDecimal(0);
    if (o != null) {
        if(o instanceof String) {
            if (((String) o).trim().length()>0) {
                value = new BigDecimal((String) o);
            }
        }
    }
    return value;
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement