java.lang.NullPointerException错误,使用[重复]

时间:2022-08-20 20:21:12

This question already has an answer here:

这个问题在这里已有答案:

I'm writing a code that finds the desired column in an input sheet and takes the entire column and put it in a different order in an output sheet. Basically I want to take in an Excel file and reorganize it in an output file. The path I've chosen is to find the numerical value of the column and use that to find the cells within each row. I'm getting an error at the line rdrCell = inputSheet.getRow(placeRow+1).getCell(y); is there something wrong with the way I'm trying to get the cell value?

我正在编写一个代码,在输入表中找到所需的列并获取整个列并将其以不同的顺序放在输出表中。基本上我想要一个Excel文件并在输出文件中重新组织它。我选择的路径是找到列的数值,并使用它来查找每行中的单元格。我在行rdrCell = inputSheet.getRow(placeRow + 1).getCell(y);我试图获得细胞价值的方式有什么问题吗?

for(Iterator<Cell> cit = idRow.cellIterator(); cit.hasNext(); )
            {
                Cell cell = cit.next();
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String chr = "Chr";
                Row rdrRow = null;
                Cell rdrCell = null;
                if(chr.equals(cell.getStringCellValue()))
                {
                    int y = cell.getColumnIndex();
                    for(int placeRow = 0; placeRow<=rowCounter;placeRow++)
                    {
                        // error in line below
                        rdrCell = inputSheet.getRow(placeRow+1).getCell(y);
                        rdrCell.setCellType(Cell.CELL_TYPE_STRING);
                        String chrString = rdrCell.getStringCellValue();
                        Cell chrCell = outputSheet.createRow(placeRow).createCell(0);
                        chrCell.setCellValue(chrString);
                    }

                }

2 个解决方案

#1


2  

Your for loop goes too far.

你的for循环太过分了。

for(int placeRow = 0; placeRow<=rowCounter;placeRow++)

Change the middle part and check for placeRow < rowCounter instead of <=

更改中间部分并检查placeRow 而不是

#2


1  

This is happening when your placeRow is equal the last index (i mean placeRow = rowCounter), so you are trying to get the placeRow + 1 but you already reached the rows limit and it does not exist.

当你的placeRow等于最后一个索引(我的意思是placeRow = rowCounter)时会发生这种情况,所以你试图获得placeRow + 1,但是你已经达到了行限制并且它不存在。

I suggest you to remove this + 1 and just use:

我建议你删除这个+ 1,然后使用:

rdrCell = inputSheet.getRow(placeRow).getCell(y);

rdrCell = inputSheet.getRow(placeRow).getCell(y);

So if you want to jump first line (like it`s happening), just start your iterator at index 1, like: int placeRow = 1

因此,如果你想跳第一行(就像它发生的那样),只需在索引1处启动你的迭代器,如:int placeRow = 1

#1


2  

Your for loop goes too far.

你的for循环太过分了。

for(int placeRow = 0; placeRow<=rowCounter;placeRow++)

Change the middle part and check for placeRow < rowCounter instead of <=

更改中间部分并检查placeRow 而不是

#2


1  

This is happening when your placeRow is equal the last index (i mean placeRow = rowCounter), so you are trying to get the placeRow + 1 but you already reached the rows limit and it does not exist.

当你的placeRow等于最后一个索引(我的意思是placeRow = rowCounter)时会发生这种情况,所以你试图获得placeRow + 1,但是你已经达到了行限制并且它不存在。

I suggest you to remove this + 1 and just use:

我建议你删除这个+ 1,然后使用:

rdrCell = inputSheet.getRow(placeRow).getCell(y);

rdrCell = inputSheet.getRow(placeRow).getCell(y);

So if you want to jump first line (like it`s happening), just start your iterator at index 1, like: int placeRow = 1

因此,如果你想跳第一行(就像它发生的那样),只需在索引1处启动你的迭代器,如:int placeRow = 1