Skip to content

Commit 0197f8c

Browse files
authored
Merge pull request #514 from ursjoss/fix/465-handle-missing-r-attribute
fix: [#465] Avoid NPE with missing 'r' attribute
2 parents ba6d2db + b1b134a commit 0197f8c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/RowSpliterator.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class RowSpliterator implements Spliterator<Row> {
3232
private final HashMap<Integer, BaseFormulaCell> sharedFormula = new HashMap<>();
3333
private final HashMap<CellRangeAddress, String> arrayFormula = new HashMap<>();
3434
private int rowCapacity = 16;
35+
private int trackedRowIndex = 0;
3536

3637
public RowSpliterator(ReadableWorkbook workbook, InputStream inputStream) throws XMLStreamException {
3738
this.workbook = workbook;
@@ -82,7 +83,10 @@ private Row next() throws XMLStreamException {
8283
if (!"row".equals(r.getLocalName())) {
8384
throw new NoSuchElementException();
8485
}
85-
int rowIndex = r.getIntAttribute("r");
86+
87+
int trackedColIndex = 0;
88+
int rowIndex = getRowIndexWithFallback(++trackedRowIndex);
89+
8690
List<Cell> cells = new ArrayList<>(rowCapacity);
8791
int physicalCellCount = 0;
8892

@@ -91,7 +95,7 @@ private Row next() throws XMLStreamException {
9195
break;
9296
}
9397

94-
Cell cell = parseCell();
98+
Cell cell = parseCell(trackedColIndex++);
9599
CellAddress addr = cell.getAddress();
96100
ensureSize(cells, addr.getColumn() + 1);
97101

@@ -102,9 +106,20 @@ private Row next() throws XMLStreamException {
102106
return new Row(rowIndex, physicalCellCount, cells);
103107
}
104108

105-
private Cell parseCell() throws XMLStreamException {
106-
String cellRef = r.getAttribute("r");
107-
CellAddress addr = new CellAddress(cellRef);
109+
private int getRowIndexWithFallback(int fallbackRowIndex) {
110+
Integer rowIndexOrNull = r.getIntAttribute("r");
111+
return rowIndexOrNull != null ? rowIndexOrNull : fallbackRowIndex;
112+
}
113+
114+
private CellAddress getCellAddressWithFallback(int trackedColIndex) {
115+
String cellRefOrNull = r.getAttribute("r");
116+
return cellRefOrNull != null ?
117+
new CellAddress(cellRefOrNull) :
118+
new CellAddress(trackedRowIndex, trackedColIndex);
119+
}
120+
121+
private Cell parseCell(int trackedColIndex) throws XMLStreamException {
122+
CellAddress addr = getCellAddressWithFallback(trackedColIndex);
108123
String type = r.getOptionalAttribute("t").orElse("n");
109124
String styleString = r.getAttribute("s");
110125
String formatId = null;

fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/FastExcelReaderTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.apache.poi.ss.usermodel.Workbook;
1919
import org.apache.poi.ss.usermodel.WorkbookFactory;
20+
import org.apache.poi.util.IOUtils;
2021
import org.apache.poi.xssf.usermodel.XSSFCell;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.params.ParameterizedTest;
@@ -155,6 +156,7 @@ private List<RowDates> readUsingFastExcel() throws IOException {
155156
"/xlsx/write.xlsx",
156157
"/xlsx/issue143.xlsx",
157158
"/xlsx/issue161.xlsx",
159+
"/xlsx/issue514.xlsx",
158160
// "/xlsx/xlsx-stream-d-date-cell.xlsx"
159161
})
160162
void testFile(String file) {
Binary file not shown.

0 commit comments

Comments
 (0)