简体   繁体   English

Apache POI获取NPE读取xls文件

[英]Apache POI gets NPE reading xls file

I'm trying to read an XLS file into java, which looks something like this 我正在尝试将一个XLS文件读入java,看起来像这样

Column A|Product ID|Number Sold A栏|产品编号|已售出数量
.................|105029 ....|15 ................. | 105029 .... | 15
.................|102930 ....|9 ................. | 102930 .... | 9
.................|203911 ....|29 ................. | 203911 .... | 29
.................|105029 ....|4 ................. | 105029 .... | 4

where I need to add up the total number of product sold for each product ID and then create a new file with the data sorted. 我需要在其中添加每个产品ID的销售产品总数,然后创建一个新数据,并对数据进行排序。 This program is supposed to be flexible, as in there could be 1000 different product ID's or 400. The code below is what I have so far... but there are quite a few problems with it and my lack of java knowledge is making it really frustrating. 这个程序应该是灵活的,因为可能有1000个不同的产品ID或400.下面的代码是我到目前为止...但它有相当多的问题,我缺乏java知识正在使它真的很沮丧。

The first for loop does not continue, it's stuck at r=1, although the second for loop continues. 第一个for循环没有继续,它停留在r = 1,尽管第二个for循环继续。

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Read {

            public static void readXLSFile() throws IOException{
           InputStream ExcelFile = new FileInputStream("C:/Sales Data.xls");
                HSSFWorkbook wb = new HSSFWorkbook(ExcelFile);
                HSSFSheet sheet=wb.getSheetAt(0);

                int numRows = sheet.getPhysicalNumberOfRows(); 
                //to intialize an array
                int[]idAccum = new int[numRows]; 
                //holds the product id
                int[]saleAccum = new int[numRows]; 
                //holds the total sales per product ID

                        for(int r=1;r<=numRows;r++){ 
                        //iterates through the product ID and matching sales
                            for(int j=r+1;j<numRows+1; j++){
                            HSSFCell rows = sheet.getRow(r).getCell(1);
                            HSSFCell cells = sheet.getRow(r).getCell(2);
                            HSSFCell rows1 = sheet.getRow(j).getCell(1);
                            HSSFCell cells1 = sheet.getRow(j).getCell(2);
                                if(rows==rows1){ 
                            //compares product ids
                                    idAccum[r]=rows1.getNumericCellValue(); 
                        //places product id in element r if rows and rows1 match
                                    saleAccum[r]+=cells.getNumericCellValue(); 
                        //adds number of items sold to corresponding product ID
                                }
                            }
                            System.out.println(idAccum[r]); 
                            System.out.println(saleAccum[r]);
                        }   
            }

            public static void main(String[] args) throws IOException {
        readXLSFile();

            }
}

But I'm getting nullpointexceptions. 但我得到了nullpointexceptions。

Exception in thread "main" java.lang.NullPointerException 线程“main”java.lang.NullPointerException中的异常
at Read.readXLSFile(Read.java:29) 在Read.readXLSFile(Read.java:29)
at Read.main(Read.java:45) 在Read.main(Read.java:45)
Java Result: 1 Java结果:1

You have an off-by-one error: r goes up to numRows , and j starts out at r + 1 – which during the last iteration is numRows + 1 . 你有一个一个一个错误: r上升到numRowsjr + 1 - 在最后一次迭代期间是numRows + 1 Since there is no content in that row, getCell(…) on it will return null (as per the API definition). 由于该行中没有内容,因此其上的getCell(…)将返回null (根据API定义)。 This is where the NullPointerException is coming from. 这是NullPointerException的来源。

Change 更改

for(int r=1;r<=numRows;r++){

to

for(int r=1;r<numRows;r++){

to get rid of the error. 摆脱错误。 Also defensive programming (ie checking getCell(…) results for null is a good idea. 同样防御性编程(即检查null getCell(…)结果是一个好主意。

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

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