java.lang.NumberFormatException:对于输入字符串:";空"。。can';我不明白为什么

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

尽管我在Youtube上跟踪某人并复制他的代码,但我还是不断收到这个错误。他没有犯错误,但我知道

以下代码在类
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());
I
order.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;