首页 > 解决方案 > JTable 中 SUM 所有数据为 NULL 的问题

问题描述

我正在使用 Java。我有各种数据JTable。我希望对所有内容进行求和,但其中包含 NULL。

float sum_OverTime_C = 0;
for (int i = 0; i < jTable_3.getRowCount(); i++) {
    sum_OverTime_C = sum_OverTime_C + Float.parseFloat(jTable_3.getValueAt(i, 1).toString());
}
jLabel_Total_OverTime.setText(Float.toString(sum_OverTime_C));

标签: javaswingnullsumjtable

解决方案


您必须在求和之前检查该值是否null为,例如:

if(jTable_3.getValueAt(i, 1) != null){ 
   sum_OverTime_C += Float.parseFloat(jTable_3.getValueAt(i, 1).toString());
}

推荐阅读