保存到excel文件会导致java.lang.NullPointerException

时间:2015-09-09 08:56:43

标签: java excel nullpointerexception

我尝试将数据保存到Excel文件中。我创建了excel文件来添加标题等

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");                               
        }

}              

在cosole中

recorddssss [the eleements are printed fine]
sizee is 38
i: 0
Exceptionn: java.lang.NullPointerException

经过几个小时思考这个异常来自哪里(因为我在另一个方法中使用了一个不同的文件做了同样的事情并且它工作得很好),我用那个替换了excel文件然后工作正常。我不明白为什么新文件不起作用,旧的文件正在工作..我检查了两个文件的属性,他们有相同的安全属性

0 个答案:

没有答案