java - 从 excel XSSFCell cell = row.getCell(j) 读取时获取空指针异常 java;

标签 java apache-poi

private String[][] data;

// to read from excel file

public void readFile() throws IOException {

    // Path to the excel file
    // File datafile = new File("C:\\Users\\atomar\\Documents\test.xlsx");
    // A Buffered File Input Stream to read the data
    FileInputStream f = new FileInputStream(
            "C:\\Users\\atomar\\Documents\\test.xlsx");
    System.out.println("file found");
    // InputStream fis = new BufferedInputStream(new FileInputStream("f"));
    // We create a workbook which represents the excel file
    XSSFWorkbook book = new XSSFWorkbook(f);

    // Next a sheet which represents the sheet within that excel file
    XSSFSheet sheet = book.getSheet("Sheet1");

    // No of rows in the sheet
    int rowNum = sheet.getLastRowNum() + 1;
    System.out.println("rowNum");

    // No of columns in the sheet
    int colNum = sheet.getRow(0).getLastCellNum();

    // A Two dimensional array of Strings which represents the data in the
    // sheet
    data = new String[rowNum][colNum];
    {

        for (int i = 0; i < rowNum; i++) {
            // Get the row
            XSSFRow row = sheet.getRow(i);

        for (int j = 0; j < colNum; j++) {
            // Get the columns or cells for the first row and keep
                // looping
                // for the other rows
        XSSFCell cell = row.getCell(j);

        // Make a call to the method cellToString which actually
                // converts the cell contents to String
                data[i][j] = cellToString(cell);

            //  data[i][j] = value;

    // Here is where you write the logic to handle the data.I am
                // just printing out the contents here.

                //System.out.println("The value is " + data[i][j]);


            }
        }

我在这里调用那个函数

public void InterestedParty() throws InterruptedException {
    try {
        readFile();
    } catch (IOException e1) {
    }
}

出现错误:不支持这种类型的单元格 我从输入的 excel 文件中删除了两行数据,然后它在工作正常之前就开始了。但是现在我已经完全删除了那些行,还有问题

最佳答案

如果没有数据,XSSFCell 将为null。检查它是否为 null。在调用您的 cellToString() 方法之后。

    XSSFCell cell = row.getCell(j);
    if(cell != null) {
        data[i][j] = cellToString(cell);
    } else {
        // if you would like to set value when cell is null
        row.createCell(j).setCellValue(your value);
    }

关于java - 从 excel XSSFCell cell = row.getCell(j) 读取时获取空指针异常 java;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156383/

相关文章:

java - 想要在 openshift 上托管 JAVA 中的原始套接字程序

java - 如何使用 apache poi 检查 xlsx 文件是否受密码保护

java - 使用 Apache POI 4.1.1 从 excel 文件中读取值时出错

java - 线程中的异常 "AWT-EventQueue-0"java.lang.NoSuchMethodError : org. apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V

java - 使用 POI 获取应用于 Excel 单元格的货币

java - 如何检查输入元素是否在表单元素内

java - 使用red5服务器实现桌面屏幕共享

java - 不使用 Thread.Join() ,我怎样才能实现相同的功能

jasper-reports - 如何设置Jasper使用XSSF?

java - future 的取消方法文档