简体   繁体   English

使用POI引发java.lang.NullPointerException

[英]Using POI throws java.lang.NullPointerException

I am trying to read data from one .xlsx and put it in a different file. 我正在尝试从一个.xlsx读取数据并将其放在另一个文件中。 The error happens when the loop is running for the second time. 该错误在循环第二次运行时发生。 There are no blank cells and all the 187 rows have data in them (checked and verified). 没有空白单元格,所有187行中都有数据(已检查并验证)。 There is only one column with data in it (the first column A), so i am not using the Null Checker. 只有一列包含数据(第一列A),所以我没有使用Null Checker。 Here is the code that i have so far. 这是我到目前为止的代码。 I am not including the code for writing and closing the file as the error occurs during the second iteration of the loop. 我不包括用于写入和关闭文件的代码,因为在循环的第二次迭代期间发生了错误。

FileInputStream fis=new FileInputStream("C:\\test\\Migration_input.xlsx");


    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheetAt(0);
    int rowCount = sheet.getLastRowNum();
    System.out.println(rowCount);

    FileOutputStream fos=new FileOutputStream("C:\\test\\Migration.xlsx");

    for (int b=0; b<rowCount;b++)
    {

        System.out.println(b);

        //This part to read from existing file

        XSSFRow rowreader=sheet.getRow(b);
        XSSFCell cellreader = rowreader.getCell(0);
        **String cellinput = cellreader.getStringCellValue(); //Errors out on this line on second iteration - It is not a null value and has some string data in it.

        //This part to write in a different file

        XSSFRow rowwriter=sheet.createRow(b+1);
        XSSFCell cellwriter= rowwriter.createCell(2);
        System.out.println(cellinput);
        cellwriter.setCellType(cellwriter.CELL_TYPE_STRING);
        cellwriter.setCellValue(cellinput);
        System.out.println("done");     
    }

I changed the code to this but still seems to be having an error: 我将代码更改为此,但似乎仍然有错误:

for (int b=0; b<rowCount;b++)
        {

        System.out.println(b);

        //This part to read from existing file
        XSSFRow rowreader=sheet.getRow(b);
        XSSFCell cellreader = rowreader.getCell(0);
        System.out.println(cellreader.getStringCellValue());
        **cellinput[b] = cellreader.getStringCellValue();   //Errors out    
        System.out.println("stored in array string all the values");
        }

And here is the updated output 这是更新的输出

0
Academic Cert Expected Date
FAILED: get_labels
java.lang.NullPointerException
at crm_migration.labels_on_given_object.get_labels(labels_on_given_object.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)

It reads the value from the .xlsx fine but when i try to store it into an string array, that is when it throws the error now. 它从.xlsx精细读取值,但是当我尝试将其存储到字符串数组中时,即它立即抛出错误。 I have separated the loop so that it only reads now and stores the value in an array, **again there are no null values in the first column A with 187 rows in it. 我已经将循环分开,以使其仅现在读取并将值存储在数组中,**同样,在第一列A中没有空值,其中有187行。 Please assist. 请协助。

The read logic you have implemented is wrong.First create a loop over all the rows and then loop through cell values in each row 您实现的读取逻辑是错误的,首先在所有行上创建一个循环,然后遍历每行中的单元格值

//This part to read from existing file

   //Create a loop over all the rows of excel file to read it

    for (int b=0; b<=rowCount;b++)
            {

                Row row = sheet.getRow(b);

                //loop to print cell values in a row

                for (int j = 0; j < row.getLastCellNum(); j++) {

                    String cellinput=row.getCell(j).getStringCellValue();

                    System.out.println(cellinput);
    }
    }

Similarly implement the write logic 类似地实现写逻辑

I have tested the above code it works fine.Kindly get back if you have any problems 我已经测试了上面的代码,它可以正常工作。如果您有任何问题,请回来

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

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