java.lang.NumberFormatException:对于输入字符串:“null”

时间:2017-12-28 07:33:48

标签: java android

我一直收到这个错误:( 错误出现在Donasi.class

这是在Donasi班:

 isHidden() {
  let list = ['/users'],
  route = this.location.path();
  return (list.indexOf(route) > -1);
};

这是在DonationAdapter类中:

 int total = 0;
    for (Transaksi transaksi:donasi)
        total += (Integer.parseInt(transaksi.getMinimal()))*(Integer.parseInt(transaksi.getQuantity()));
        Locale locale = new Locale("in", "ID");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
        txtTotalDonasi.setText(fmt.format(total));

这是我的Logcat:

    int minimal = (Integer.parseInt(listData.get(position).getMinimal()))*(Integer.parseInt(listData.get(position).getQuantity()));

    holder.txt_minimal.setText(fmt.format(minimal));

    holder.txt_nama_donasi.setText(listData.get(position).getNamaDonasi());

如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

struct _class { var kind: Int var superclass: Any.Type? var reserveword1: Int var reserveword2: Int var databits: UInt // other fields we don't care func class_rw_t() -> UnsafePointer<_class_rw_t>? { if MemoryLayout<Int>.size == MemoryLayout<Int64>.size { let fast_data_mask: UInt64 = 0x00007ffffffffff8 let databits_t: UInt64 = UInt64(self.databits) return UnsafePointer<_class_rw_t>(bitPattern: UInt(databits_t & fast_data_mask)) } else { return UnsafePointer<_class_rw_t>(bitPattern: self.databits & 0xfffffffc) } }} struct _class_rw_t { var flags: Int32 var version: Int32 var ro: UInt func class_ro_t() -> UnsafePointer<_class_ro_t>? { return UnsafePointer<_class_ro_t>(bitPattern: self.ro) }} struct _class_ro_t { var flags: Int32 var instanceStart: Int32 var instanceSize: Int32} class TestModel {} let c = TestModel() let pointer = unsafeBitCast(TestModel.self, to: UnsafePointer<_class>.self) let instanceStart = pointer.pointee.class_rw_t()?.pointee.class_ro_t()?.pointee.instanceStart transaksi.getMinimal()为空

请在尝试transaksi.getQuantity()

之前进行检查

也许喜欢

parseInt

答案 1 :(得分:1)

select t1.rid, 

((t1.avg_num_votes*t1.avg_rating)+(t1.this_num_votes*t1.this_rating))/(t1.avg_num_votes+t1.this_num_votes) as rating

from (

SELECT

    rid,

    (SELECT count(rid) FROM visits) / (SELECT count(DISTINCT rid) FROM visits) AS avg_num_votes,

    (SELECT avg(rating) FROM visits) AS avg_rating,

    count(rid) as this_num_votes,

    avg(rating) as this_rating

FROM

    visits

    where rating>0

GROUP BY 

    rid

    ) as t1

    Order by rating desc
  

NumberFormatException是您可能抛出的异常   尝试将String转换为数字,其中该数字可能是一个数字   int,float或任何其他Java数字类型

您正在 Caused by: java.lang.NumberFormatException: For input string: "null" 。哪个不是 null 。添加 int 检查

null

Debug您的应用。在 total += (Integer.parseInt(transaksi.getMinimal()))*(Integer.parseInt(transaksi.getQuantity())); // Problem coming from this end 旁边添加 BREAK-POINT

添加 total 阻止

try-catch

答案 2 :(得分:0)

在解析之前,尝试在getMinimal()和getQuantity()上检查null。

if (transaksi.getMinimal() != null && transaksi.getQuantity() != null) {
 total += (Integer.parseInt(transaksi.getMinimal()))*
        (Integer.parseInt(transaksi.getQuantity()));
}

OR

将解析代码保留在try块中并捕获NumberFormatException。当getMinimal()或getQuantity()不为null而不是数字字符串

时,这也有帮助
try {
 total += (Integer.parseInt(transaksi.getMinimal()))*
        (Integer.parseInt(transaksi.getQuantity()));
}catch(NumberFormatException e){
Log.e("TAG","Excepting parsing Integer from string");
}

答案 3 :(得分:-1)

transaksi.getMinimal()transaksi.getQuantity()应该是有效的数字字符串,以解析为整数

if (transaksi.getMinimal() != null && transaksi.getQuantity() != null && transaksi.getMinimal().equalIgnoreCase("null") && transaksi.getQuantity() .equalIgnoreCase("null") && !transaksi.getMinimal().isEmpty() && !transaksi.getQuantity().isEmpty()) {
     total += (Integer.parseInt(transaksi.getMinimal()))*
            (Integer.parseInt(transaksi.getQuantity()));
}