简体   繁体   English

Jxl NullPointerException与workbook.close()方法有关

[英]Jxl NullPointerException to do with workbook.close() method

I am attempting to write to an excel file, but I have encountered two problems. 我正在尝试写入excel文件,但是遇到了两个问题。 Here is the relevant code: 以下是相关代码:

public ExcelFormWriter() throws IOException, WriteException{
  setOutputFile("DataAnalysis.xls");

    File file = new File(inputFile);
    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale("en", "EN"));
    workbook = Workbook.createWorkbook(file, wbSettings);
    workbook.createSheet("RawData", 0);

  }

  public void addLabel(int column, int row, String s) throws WriteException, RowsExceededException, IOException {
    excelSheet = workbook.getSheet(0);
    createLabel(excelSheet);
    Label label = new Label(column, row, s, times);
    excelSheet.addCell(label);
    workbook.write();
    workbook.close();
  }

In another class, I call it like this: 在另一个类中,我这样称呼它:

    ExcelFormWriter test = new ExcelFormWriter();
 int row = 1;
        for (int i = 100; i > 0; i--){
            test.addLabel(0,row,getDateNDaysAgo(i));
            row++;
        }

If I run it just like this, it writes the first line in the excel file and then returns a NullPointerException. 如果我像这样运行它,它将在Excel文件中写入第一行,然后返回NullPointerException。

I found that if I comment out the 'workbook.close()', it runs through the for loop and the build finishes successfully, but when I look in the excel file, nothing is actually written. 我发现如果我注释掉'workbook.close()',它将在for循环中运行并且构建成功完成,但是当我查看excel文件时,实际上什么也没写。

I'm not entirely familiar with writing to spreadsheets, so it's likely that there's a basic flaw in my code, like something being in the wrong place. 我对写入电子表格并不完全熟悉,因此我的代码中可能存在一个基本缺陷,例如放置在错误的位置。 Any answer is appreciated, but please be specific in your answers, because I'm feeling around in the dark with this code and I don't know what does what. 感谢您提供任何答案,但请在您的答案中具体说明,因为我对这段代码感到困惑,而且我不知道该做什么。

Suggestions for future diagnostics/troubleshooting of this stuff would also be appreciated. 对于将来对这些东西的诊断/故障排除的建议也将不胜感激。

UPDATE UPDATE

I have put in a separate method for closing the workbook. 我已经提出了一种单独的方法来关闭工作簿。 Thanks for the tip. 谢谢你的提示。 However, the second problem persists. 但是,第二个问题仍然存在。 Only the first line of the spreadsheet is written. 仅写入电子表格的第一行。

  public void close() throws IOException, WriteException{
      workbook.close();
  }

This is where I called the method. 这就是我所谓的方法。 Perhaps there's a problem with that? 也许有问题吗?

int row = 1;
        for (int i = 100; i > 0; i--){
            test.addLabel(0,row,getDateNDaysAgo(i));
            row++;
        }
        test.close();

You have to put the workbook.close() statement outside of your addLabel function, because you are calling it in the first iteration of the for-loop, closing the workbook and then on the next iteration you are trying to write to an no longer existing workbook. 您必须将workbook.close()语句放在addLabel函数之外,因为您是在for循环的第一个迭代中调用它,关闭工作簿,然后在下一个迭代中尝试不再写入现有工作簿。 Add an closeWorkbook Function or call the Workbook by an getter and close it after you've written your cells. 添加closeWorkbook函数或由吸气剂调用工作簿,并在编写单元格后将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到符号Workbook.close() - cannot find symbol Workbook.close() 为什么Apache poi的Workbook.close()方法将内容写入输入文件? - Why does Apache poi's Workbook.close() method write content to input file? Apache POI SXSSF - Workbook.close() 清除先前保存的文件中的第二个(最后一个)工作表数据 - Apache POI SXSSF - Workbook.close() clears the second (the last) sheet data in previously saved file Apache POI:运行工作簿创建代码两次会出现错误“工作表已存在”,即使在 workbook.close() 之后也是如此 - Apache POI : Running the workbook creation code twice gives error “Sheet already exists”, even after workbook.close() Java Jxl读/写到工作簿 - Java Jxl read/writing to workbook jxl中程序关闭时数据损坏 - Data corrupting on program close in jxl 我如何使用jxl定期刷新Excel中的行,同时最后关闭 - How do i periodically flush the rows in excel using jxl while do the close at the last 使用 java jxl 在现有工作簿中添加新工作表 - Add a new sheet in an existing workbook with java jxl Getting Out of memory when reading large excel files (.xls) using Workbook method of apache poi jxl-2.6.jar - Getting Out of memory when reading large excel files (.xls) using Workbook method of apache poi jxl-2.6.jar 如何在硒webdriver中使用JXL来检查工作簿表是否存在? - How to check in workbook sheet exist or not using JXL in selenium webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM