java.lang.NumberFormatException:对于输入字符串:";空"。。can';我不明白为什么
尽管我在Youtube上跟踪某人并复制他的代码,但我还是不断收到这个错误。他没有犯错误,但我知道 以下代码在类java.lang.NumberFormatException:对于输入字符串:";空"。。can';我不明白为什么,java,android,Java,Android,尽管我在Youtube上跟踪某人并复制他的代码,但我还是不断收到这个错误。他没有犯错误,但我知道 以下代码在类Cart // Calculating the total price int total = 0 ; for(Order order:cart) total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity())); Locale l
Cart
// Calculating the total price
int total = 0 ;
for(Order order:cart)
total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("en" , "US" );
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
以下代码在类CartAdapter
int price = (Integer.parseInt(listData.get(position).getPrice()))
*(Integer.parseInt(listData.get(position).getQuantity()));
holder.txt_price.setText(fmt.format(price));
holder.txt_cart_name.setText(listData.get(position).getProductName());
Iorder.getPrice()
可能是null
。而order.getQuantity()
可能是null
如果其中一个或两个为空,将导致NumberFormatException
所以你可以这样做
如果你想用的话
total += (Integer.parseInt(TextUtils.isEmpty(order.getPrice()) ? "0" : order.getPrice())) * (Integer.parseInt(TextUtils.isEmpty(order.getQuantity()) ? "0" : order.getQuantity()));
及
注意
您可以这样做,如果您的响应为空,您可以返回一个默认值。它不会导致NumberFormatException
TextUtils.isEmpty(response) ? "0" : response;
我认为您需要显示更多的代码,并显示完整的错误
TextUtils.isEmpty(response) ? "0" : response;