简体   繁体   English

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

[英]getting null pointer exception java while reading from excel XSSFCell cell = row.getCell(j);

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]);


            }
        }

Here I am calling that function 在这里我叫那个功能

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

Getting error: There are no support for this type of cell I deleted two row data from input excel file then it started before it was working fine. 收到错误消息:不支持这种类型的单元格,我从输入excel文件中删除了两行数据,然后在其正常工作之前就开始了。 But now I have deleted those row completely the also issue is there 但是现在我已经完全删除了那些行,还有问题

if there is no data, XSSFCell will be null . 如果没有数据,则XSSFCell将为null Check whether it is null or not. 检查它是否为null After call your cellToString() method. 之后,调用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);
    }

You can have a row with data, an empty row next then another row with data. 您可以有一行数据,下一行为空,然后另一行为数据。 The middle row can be viewed as null if is undefined, so my advice is to use an iterator for the rows. 如果未定义中间行,则可以将其视为null,因此我的建议是为行使用迭代器。
RowIteratior here is a good start. RowIteratior是一个好的开始。
Also, once you have the row, you can use cell iterator to iterate over the cells. 同样,一旦有了该行,就可以使用单元格迭代器遍历单元格。 Keep in mind that an undefined cell will be viewed as null and skipped, but an empty cell is not null! 请记住,未定义的单元格将被视为null并被跳过,但是空白单元格不为null!
Cell iterator is doc'ed here. 单元迭代器在此处记录。
Your approach is also good, but you need to check for nulls or empty at each iteration before working with the objects. 您的方法也不错,但是在使用对象之前,您需要在每次迭代中检查是否为空或为空。

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

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