java - Apache POI gets NPE reading xls file -


i'm trying read xls file java, looks this

column a|product id|number sold
.................|105029 ....|15
.................|102930 ....|9
.................|203911 ....|29
.................|105029 ....|4

where need add total number of product sold each product id , create new file data sorted. program supposed flexible, in there 1000 different product id's or 400. code below have far... there quite few problems , lack of java knowledge making frustrating.

the first loop not continue, it's stuck @ r=1, although second loop continues.

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 array                 int[]idaccum = new int[numrows];                  //holds product id                 int[]saleaccum = new int[numrows];                  //holds total sales per product id                          for(int r=1;r<=numrows;r++){                          //iterates through product id , 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 , rows1 match                                     saleaccum[r]+=cells.getnumericcellvalue();                          //adds number of items sold 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.

exception in thread "main" java.lang.nullpointerexception
@ read.readxlsfile(read.java:29)
@ read.main(read.java:45)
java result: 1

you have off-by-one error: r goes numrows, , j starts out @ r + 1 – during last iteration numrows + 1. since there no content in row, getcell(…) on return null (as per api definition). nullpointerexception coming from.

change

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

to

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

to rid of error. defensive programming (i.e. checking getcell(…) results null idea.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -