我正在尝试更新我的excel文件,但出于某种原因,我在第二次尝试时从单元格中获取null。
public class ExcelWriter {
XSSFSheet sheet;
public ExcelWriter(List<String> nurses, List<String> therapist) {
FileInputStream file;
try {
file = new FileInputStream("Schedule.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(file);
sheet = workbook.getSheetAt(0);
for (int i=0;i<nurses.size();i++)
{
writeToCell(i+1,0,nurses.get(i));
}
for (int i=0;i<therapist.size();i++)
{
writeToCell(i+10,0,nurses.get(i)); //Getting null here
}
file.close();
FileOutputStream outFile =new FileOutputStream(new File("Schedule.xlsx"));
workbook.write(outFile);
outFile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeToCell(int row, int col, String string) {
Cell cell = null;
XSSFRow sheetrow = sheet.getRow(row);
cell = sheetrow.getCell(col); //Getting null here
cell = sheetrow.createCell(col);
cell.setCellValue(string);
}
}
日志:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.michlind.cheduler.ExcelWriter.writeToCell(ExcelWriter.java:66)
at com.michlind.cheduler.ExcelWriter.<init>(ExcelWriter.java:40)
at com.michlind.cheduler.MainClass$2.actionPerformed(MainClass.java:343)