Blank cells throwing null pointer exception while reading
I am using this api and rows contains blank cells causing null pointer issue. Below is the stack trace. com.monitorjbl.xlsx.exceptions.NotSupportedException java.lang.NullPointerException at test.XLSXToCSVConverterStreamer.xlsx(XLSXToCSVConverterStreamer.java:67) at test.XLSXToCSVConverterStreamer.main(XLSXToCSVConverterStreamer.java:164)
if I use Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK) to read empty cells as blank I am getting below issue. Kindly help me in resolving this
com.monitorjbl.xlsx.exceptions.NotSupportedException at com.monitorjbl.xlsx.impl.StreamingRow.getCell(StreamingRow.java:108)
I had a similar issue, I solved it like this:
Cell cell = row.getCell(i);
if (cell == null) {
cell = new StreamingCell(i, row.getRowNum());
}
this just creates a StreamingCell with null value and you can go from there.
edit// formatting (indentation does not seem to work here)
@andreasellervee thank you so much for your post. It helped me a lot. I was at the same issue and I could fix it by using your solution. I realized when we use StreamingReader and iterate cells of a row, it does not consider cells with blank value, shifting to the next cell with valid value and messing the rest of columns out.
Just to exempify, I was considering the code below:
for (Row row : reader) {
String[] recordArray = new String[totalColumns];
int columnSheet = 0;
for (Cell cell : row) {
recordArray[columnSheet] = cell.getStringCellValue();
columnSheet++;
}
listRegistration.add(recordArray);
}
than I've changed to this, in order to work well:
for (Row row : reader) {
String[] recordArray = new String[totalColumns];
int columnSheet = 0;
for (int i=0; i<totalColumns; i++) {
Cell cell = row.getCell(i);
if (cell == null) {
cell = new StreamingCell(i, row.getRowNum());
}
recordArray[columnSheet] = cell.getStringCellValue();
columnSheet++;
}
listRegistration.add(recordArray);
}
Hello abhilashreddy1289
Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK) is now supported and resolves the issue.
Hope this is good news.
Row.CREATE_NULL_AS_BLANK not working
Can you confirm that you are using the latest maven version 1.1.0.
If you are please provide the sheet and code. Then we can help you.
@waxxxd
Any thoughts on implementing setMissingCellPolicy:
@Override
public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
throw new UnsupportedOperationException();
}
on StreamingWorkbook? Then it wouldn't have to be passed in for every getCell call.
Hello @dlandis
I haven't really looked into the code too much. Committed the change to fix a bug I had encountered in a personal project. Have a crack at it.