Reputation:
Hello everyone,
i want to read cells from a Excelsheet and put the data in a Arraylist. But get i a NullpointerException
. I am really frustated, because in an another Excelsheet it works but now not. The Excelsheet self is not empty or something like this.
Is there something what i don't see?
Here is my Code:
List<Cell> gyro = new ArrayList();
File filegyro = new File("./data/Gyro_convert.xlsx");
FileInputStream fisgyro = new FileInputStream(filegyro);
XSSFWorkbook wbgyro = new XSSFWorkbook(fisgyro);
XSSFSheet sheetgyro = wbgyro.getSheetAt(0);
Iterator iteratorgyro = sheetgyro.iterator();
while (iteratorgyro.hasNext()) {
XSSFRow row = (XSSFRow) iteratorgyro.next();
Iterator cellIterator = row.cellIterator();
Cell cell = row.getCell(5);
gyro.add(cell);
}
gyro.remove(0); // because the first cell is String
for (Cell c : gyro) {
System.out.println(c.getNumericCellValue());
}
}
Upvotes: 0
Views: 72
Reputation: 4710
If you want to omit NullPointerExceptions
just use DataFormatter
to format the cells into strings
for you. Regardless of the cell type, it will format it in the correct format and whenever the cells are null
, it just returns an empty string
.
e.g.
DataFormatter dataFormatter = new DataFormatter();
for (Cell c : gyro) {
System.out.println(dataFormatter.formatCellValue(c);
}
And debugging may help, set breakpoints whenever you have such problems.
Upvotes: 1