Arraylist替换java中的空值
我的工作流程 使用mysql驱动程序执行查询并将结果集保存在arraylist中,然后从arraylist在excel工作表中填写详细信息(使用Apache poi) 问题 如果arraylist包含null,则在excel工作表中的插入过程中会出现null指针异常 我的代码Arraylist替换java中的空值,java,arraylist,apache-poi,Java,Arraylist,Apache Poi,我的工作流程 使用mysql驱动程序执行查询并将结果集保存在arraylist中,然后从arraylist在excel工作表中填写详细信息(使用Apache poi) 问题 如果arraylist包含null,则在excel工作表中的插入过程中会出现null指针异常 我的代码 resultSet = statement.executeQuery(query); while (resultSet.next()){ Date lastResponse
resultSet = statement.executeQuery(query);
while (resultSet.next()){
Date lastResponse = resultSet.getDate("lastresponse");
response.add(lastResponse);
}
上面的arraylist包含以下值
2017-04-20, 2017-04-25, 2017-04-24, null, 2017-08-03
现在我尝试在excel工作表中插入上述值(全部)
for (int j = 0; j <= response.size(); j++) {
row = sheet.getRow(j + 2);
cell = row.createCell(1);
cell.setCellValue(Cell.CELL_TYPE_STRING);
cell.setCellValue(response.get(j).toString());
}
for(int j=0;j您的代码只需要一个空值检查。空值可以通过行进行检查
response.get(j)!=null
检查后,您可以随意更改代码
for (int j = 0; j <= response.size(); j++) {
row = sheet.getRow(j + 2);
cell = row.createCell(1);
cell.setCellValue(Cell.CELL_TYPE_STRING);
response.get(j)!=null ? cell.setCellValue(response.get(j).toString()) : cell.setCellValue("Null");
}
for(int j=0;jresponse=response.stream().map(x->(x==null)?“-”:x.collect()
为什么不在调用response.get(j)!=null之前先检查if(response.get(j)!=null)
,或者如果要在表中打印“null”,也可以使用String.valueOf(response.get(j))
。