I am iterating down a column of values until it hits a blank. I need to gather the entire column and store it, and no other values. I’ve attempted to check for blank, null, 0, “”, and CELL_TYPE_BLANK (int = 3), and I cannot get it to avoid a null pointer exception. Code snippet and error are below. What can I do? This is not the entire method or program, just the relevant piece.
String s = list[i];
//find the directory from the list array to locate the file
InputStream input = new FileInputStream(s);
//create a new workbook object to hold the excel file
Workbook wb = new XSSFWorkbook(input);
//create an arbitrary starting location
int column = 2; //absolutely the correct number
int rownum = 10;
//get the value from the first sheet
org.apache.poi.ss.usermodel.Sheet insheet = wb.getSheetAt(0);
//of the second columm
Row row = insheet.getRow(rownum);
//in the 11th row (arbitrary number used to reduce iterations and skip whitespace)
Cell cell = row.getCell(column);
System.out.println("Skimming sheet: " + insheet.getSheetName());
//iterate until the very end of the column is found
System.out.println("Cell value B" + (rownum-1) + ": " + cell);
//3 denotes CELL_TYPE_BLANK
while (cell.getCellType() != 3 ) {
//go to the next location
//update to the next cell
System.out.println("Cell value B" + rownum + ": " + cell);
row = insheet.getRow(rownum);
if(row.getCell(column).getCellType() != 3){
cell = row.getCell(column); //error occurs here, line 241
}
rownum++;
}
Exception in thread "main" java.lang.NullPointerException
at FileTest.skim(FileTest.java:241)
at FileTest.main(FileTest.java:121)
You’re gettint the error because the row you’re trying to access is null, not the cell. You want to take a look in the Apache POI Iterator example.