excel-streaming-reader icon indicating copy to clipboard operation
excel-streaming-reader copied to clipboard

Blank cells throwing null pointer exception while reading

Open abhilashreddy1289 opened this issue 9 years ago • 7 comments

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)

abhilashreddy1289 avatar Mar 03 '16 07:03 abhilashreddy1289

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 avatar Apr 05 '16 07:04 andreasellervee

@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);
        }

jhhino avatar Apr 27 '16 20:04 jhhino

Hello abhilashreddy1289

Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK) is now supported and resolves the issue.

Hope this is good news.

waxxxd avatar Feb 01 '17 11:02 waxxxd

Row.CREATE_NULL_AS_BLANK not working

moisesf10 avatar Mar 17 '17 00:03 moisesf10

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 avatar Jun 14 '17 15:06 waxxxd

@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.

dlandis avatar Feb 12 '18 22:02 dlandis

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.

waxxxd avatar Sep 10 '18 08:09 waxxxd