简体   繁体   English

使用Apache POI加载xlsx文件时发生NullpointerException

[英]NullpointerException on loading xlsx file using Apache POI

Am tryng to read a file using Apache POI 3.8 in mac osx environment, which is created in Windows using Open XML SDK 2.0 for Microsoft Office, am getting null pointer exception as it is unable to load the xlsx file. 我试图在Mac osx环境中使用Apache POI 3.8读取文件,该文件是在Windows中使用Microsoft Office的Open XML SDK 2.0创建的,但由于无法加载xlsx文件,因此出现了空指针异常。 Below is the stack trace. 下面是堆栈跟踪。 I am able to open the file and view it. 我可以打开文件并查看它。

The same code works for the files that I create in mac os env. 相同的代码适用于在mac os env中创建的文件。 If i open and save the file before processing, am not having any issue. 如果我在处理之前打开并保存文件,则没有任何问题。 Note : The File size increases after saving it. 注意:文件大小在保存后会增加。 Is it issue with file being generated in .net and being processed in mac osx environment. 在.net中生成并在mac osx环境中进行处理的文件是否存在问题?

Any clues why am i getting below error. 任何线索,为什么我变得低于错误。

Caused by: java.lang.NullPointerException
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:253)
        at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73) 

The class looks like below, with utility methods, did not copy the entire class. 该类如下所示,带有实用程序方法,未复制整个类。 but this happens at line wb = new XSSFWorkbook(bis) while creating the workbook. 但这是在创建工作簿时在wb = new XSSFWorkbook(bis)行发生的。 I tried couple of options like WorkbookFactory.create(bis) instead of XSSFWorkbook , but the error remains same, issue is mainly with the file content. 我尝试了几个选项,例如WorkbookFactory.create(bis)而不是XSSFWorkbook ,但错误仍然相同,问题主要出在文件内容上。

public class XLSXReader {

    private BufferedInputStream bis;
    private boolean hasNext = true;
    int skipLines;

    private boolean linesSkiped;

    //   The default line to start reading.
    public static final int DEFAULT_SKIP_LINES = 0;

    private XSSFWorkbook wb =null;
    private XSSFSheet sheet=null;
    private int noOfCols;

    public XLSXReader(InputStream is){
        bis = new BufferedInputStream(is);
        this.noOfCols=0; 
        try {
            wb = new XSSFWorkbook(bis); 
            sheet = wb.getSheetAt(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

// with basic utility methods ....
}

I think, your missing the class path problem. 我认为,您缺少class path问题。 Try as below. 尝试如下。 Make sure that properties directry must be in the class path . 确保properties directry必须在class path Put your doucments file in these folder. 将文件文件放在这些文件夹中。

public class WorkBookReader {
    public static void main(String[] args) {
        try {
            InputStream inp = new FileInputStream("properties/workbook.xls");
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            Row row = sheet.getRow(2);
            Cell cell = row.getCell(3);
            if (cell == null)
                cell = row.createCell(5);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue("Hello!");

            // Write the output to a file
            FileOutputStream fileOut = new FileOutputStream("properties/workbook.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM