I try to save my data to a excel file. I created the excel file to add titles etc and
private void saveFile(HttpSession session, ActionsDB actions){
ArrayList<Record> records = (ArrayList<Record>) (session.getAttribute("records"));
System.out.println("recordds "+ records.toString());
Row row;
Cell column;
int size = records.size();
System.out.println("size is "+ size);
String s1="",s2="",s3="",s4= "", s="";
try {
//Get the excel file.
FileInputStream file = new FileInputStream(new File("C:\\work\\test.xls"));
//Get workbook for XLS file.
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook.
//If there have >1 sheet in your workbook, you can change it here IF you want to edit other sheets.
HSSFSheet sheet1 = workbook.getSheetAt(0);
for(int i=0; i<size; i++){
System.out.println("i: " +i);
row = sheet1.getRow(i+1);
column = row.getCell(0);
column.setCellValue(records.get(i).getDate()); // insert date
column = row.getCell(1);
column.setCellValue(records.get(i).getTime()); //insert shift
column = row.getCell(2);
column.setCellValue(records.get(i).getShift());
column = row.getCell(3);
column.setCellValue(records.get(i).getIdMachine());
column = row.getCell(4);
column.setCellValue(records.get(i).getKaloupi());
column = row.getCell(5);
column.setCellValue(records.get(i).isStardEnd());
column = row.getCell(6);
column.setCellValue(records.get(i).hasLabel());
column = row.getCell(7);
column.setCellValue(records.get(i).getTimeCycle());
column = row.getCell(8);
column.setCellValue(records.get(i).getId());
}
file.close();
System.out.println("10");
File desktopDir = new File(System.getProperty("user.home"), "Desktop");
System.out.println(desktopDir.getPath() + " " + desktopDir.exists());
String pathToDesktop = desktopDir.getPath()+ "\\workPlanner\\print";
System.out.println(pathToDesktop);
createFolderToDesktop(pathToDesktop);
FileOutputStream out = new FileOutputStream(new File(pathToDesktop, "machinesRecords.xls"));
workbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("1111111111");
} catch (IOException e) {
e.printStackTrace();
System.out.println("2222222");
}
}
In the cosole
recorddssss [the eleements are printed fine]
sizee is 38
i: 0
Exceptionn: java.lang.NullPointerException
After many hours thinking where this exception comes from (as I have done the same thing in an other method with a diferrrent file and it works there fine), I replaced the excel file with that one and then works fine. I dont understand why with the new file doesn't work and with an old one is working.. I checked the properties both of the files and they have the same security properties
After many days, I found what was causing the null exception. The problem was when the row was null
... with the following
row = sheet1.getRow(i+1);
if(row == null){
row = sheet1.createRow(i+1);
}
the program run well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.