fixed bug 41761: NPE when serializing a workbook with empty rows

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@520416 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2007-03-20 14:59:29 +00:00
parent e3305a7043
commit bfc32c3190
3 changed files with 40 additions and 7 deletions

View File

@ -783,7 +783,7 @@ public class Sheet implements Model
int dbCellOffset = 0;
for (int block=0;block<blockCount;block++) {
rowBlockOffset += rows.getRowBlockSize(block);
cellBlockOffset += cells.getRowCellBlockSize(rows.getStartRowNumberForBlock(block),
cellBlockOffset += null == cells ? 0 : cells.getRowCellBlockSize(rows.getStartRowNumberForBlock(block),
rows.getEndRowNumberForBlock(block));
//Note: The offsets are relative to the Workbook BOF. Assume that this is
//0 for now.....

View File

@ -213,7 +213,7 @@ public class RowRecordsAggregate
//Note: Cell references start from the second row...
int cellRefOffset = (rowBlockSize-20);
for (int row=startRowNumber;row<=endRowNumber;row++) {
if (cells.rowHasCells(row)) {
if (null != cells && cells.rowHasCells(row)) {
final int rowCellSize = cells.serializeCellRow(row, pos, data);
pos += rowCellSize;
//Add the offset to the first cell for the row into the DBCellRecord.

View File

@ -19,9 +19,7 @@
package org.apache.poi.hssf.usermodel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
import junit.framework.TestCase;
@ -481,8 +479,43 @@ public class TestHSSFSheet
HSSFCell c = r.createCell((short)0);
assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex());
}
/**
*
*/
public void testAddEmptyRow() throws Exception {
//try to add 5 empty rows to a new sheet
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < 5; i++) sheet.createRow(i);
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
out.close();
workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
assertTrue("No Exceptions while reading file", true);
//try adding empty rows in an existing worksheet
String cwd = System.getProperty("HSSF.testdata.path");
FileInputStream in = new FileInputStream(new File(cwd, "Simple.xls"));
workbook = new HSSFWorkbook(in);
in.close();
assertTrue("No Exceptions while reading file", true);
sheet = workbook.getSheetAt(0);
for (int i = 3; i < 10; i++) sheet.createRow(i);
out = new ByteArrayOutputStream();
workbook.write(out);
out.close();
workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
assertTrue("No Exceptions while reading file", true);
}
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(TestHSSFSheet.class);
}