使用Apache POI加载xlsx文件时出现NullpointerException

时间:2022-07-10 20:22:54

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. Below is the stack trace. I am able to open the file and view it.

我试图在mac osx环境中使用Apache POI 3.8读取文件,该环境是在Windows中使用Open XML SDK 2.0 for Microsoft Office创建的,因为无法加载xlsx文件而导致空指针异常。下面是堆栈跟踪。我可以打开文件并查看它。

The same code works for the files that I create in 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.

相同的代码适用于我在mac os env中创建的文件。如果我在处理前打开并保存文件,则没有任何问题。注意:保存后文件大小会增加。是在.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. I tried couple of options like WorkbookFactory.create(bis) instead of XSSFWorkbook, but the error remains same, issue is mainly with the file content.

该类看起来如下,使用实用方法,没有复制整个类。但是在创建工作簿时,这发生在第wb = new XSSFWorkbook(bis)行。我尝试了几个选项,如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 ....
}

1 个解决方案

#1


0  

I think, your missing the class path problem. Try as below. Make sure that properties directry must be in the class path. Put your doucments file in these folder.

我想,你错过了类路径问题。请尝试以下方法。确保属性directry必须位于类路径中。将您的doucments文件放在这些文件夹中。

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

#1


0  

I think, your missing the class path problem. Try as below. Make sure that properties directry must be in the class path. Put your doucments file in these folder.

我想,你错过了类路径问题。请尝试以下方法。确保属性directry必须位于类路径中。将您的doucments文件放在这些文件夹中。

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