Skip to content

Commit ebcdcce

Browse files
committed
Guard against the addition of null columns
And add a quick test that the problem is fixed. This fix prevents cryptic exceptions like this one: java.lang.NullPointerException: Cannot invoke "org.scijava.table.Column.size()" because "column" is null at org.scijava.table.AbstractTable.add(AbstractTable.java:167) at org.scijava.table.AbstractTable.add(AbstractTable.java:44) at org.scijava.util.SizableArrayList.setSize(SizableArrayList.java:90) at org.scijava.table.AbstractTable.setColumnCount(AbstractTable.java:75) at org.scijava.table.AbstractTable.<init>(AbstractTable.java:66) at org.scijava.table.DefaultFloatTable.<init>(DefaultFloatTable.java:49) The above exception happens if too many initial columns are given, because the SizableArrayList under the hood sometimes tries (if a certain hack fails) to expand the size of the ArrayList by repeatedly adding nulls. The new test checks that such tables can be created with lots of initial columns, *and* that we can explicitly add null columns, without this sort of exception getting thrown.
1 parent 43de685 commit ebcdcce

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/main/java/org/scijava/table/AbstractTable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,22 @@ public void set(final String colHeader, final int row, final T value) {
164164

165165
@Override
166166
public boolean add(final C column) {
167-
if (column.size() > rowCount) rowCount = column.size();
167+
if (column != null && column.size() > rowCount) rowCount = column.size();
168168
scaleColumns();
169169
return super.add(column);
170170
}
171171

172172
@Override
173173
public void add(final int col, final C column) {
174174
super.add(col, column);
175-
if (column.size() > rowCount) rowCount = column.size();
175+
if (column != null && column.size() > rowCount) rowCount = column.size();
176176
scaleColumns();
177177
}
178178

179179
@Override
180180
public boolean addAll(final Collection<? extends C> c) {
181181
for (final C column : c) {
182-
if (column.size() > rowCount) rowCount = column.size();
182+
if (column != null && column.size() > rowCount) rowCount = column.size();
183183
}
184184
scaleColumns();
185185
return super.addAll(c);
@@ -188,7 +188,7 @@ public boolean addAll(final Collection<? extends C> c) {
188188
@Override
189189
public boolean addAll(final int col, final Collection<? extends C> c) {
190190
for (final C column : c) {
191-
if (column.size() > rowCount) rowCount = column.size();
191+
if (column != null && column.size() > rowCount) rowCount = column.size();
192192
}
193193
scaleColumns();
194194
return super.addAll(col, c);

src/test/java/org/scijava/table/DefaultFloatTableTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
package org.scijava.table;
3232

3333
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNotNull;
35+
import static org.junit.Assert.assertNull;
3436
import static org.junit.Assert.assertSame;
3537

3638
import java.util.List;
@@ -304,6 +306,14 @@ public void testInsertRows() {
304306
checkTableModifiedRows(table, values, 4, 5);
305307
}
306308

309+
@Test
310+
public void testNullColumns() {
311+
final FloatTable table = new DefaultFloatTable(100, 10);
312+
table.add(null);
313+
assertNotNull(table.get(99));
314+
assertNull(table.get(100));
315+
}
316+
307317
// TODO - Add more tests.
308318

309319
// -- Helper methods --

0 commit comments

Comments
 (0)