CHEBURASHKA
CHEBURASHKA

Reputation: 1713

How to loop through all non empty rows using apache poi?

I want to loop through all rows until I encounter null

int startRow = 4;

ArrayList<String> uniques = new ArrayList<String>();

int k = 0 ;
while (sheet.getRow(startRow+k).getCell(2) != null) {  // null pointer here ?
    uniques.add(sheet.getRow(startRow+k).getCell(2).toString());
    k++ ;
}

I get a NullPointerException.

Upvotes: 0

Views: 2183

Answers (1)

rgettman
rgettman

Reputation: 178263

You are checking the Cell if it's null, but not the Row itself.

while (sheet.getRow(startRow+k) != null &&
       sheet.getRow(startRow+k).getCell(2) != null) { 

Upvotes: 3

Related Questions