Skip to content

Commit 1f568b0

Browse files
committed
prevent Table.sort() from throwing NPE with empty cells
1 parent 2ae4e0e commit 1f568b0

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

core/src/processing/data/Table.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4350,7 +4350,15 @@ public int compare(int index1, int index2) {
43504350
double diffd = getDouble(a, column) - getDouble(b, column);
43514351
return diffd == 0 ? 0 : (diffd < 0 ? -1 : 1);
43524352
case STRING:
4353-
return getString(a, column).compareToIgnoreCase(getString(b, column));
4353+
String string1 = getString(a, column);
4354+
if (string1 == null) {
4355+
string1 = ""; // avoid NPE when cells are left empty
4356+
}
4357+
String string2 = getString(b, column);
4358+
if (string2 == null) {
4359+
string2 = "";
4360+
}
4361+
return string1.compareToIgnoreCase(string2);
43544362
case CATEGORY:
43554363
return getInt(a, column) - getInt(b, column);
43564364
default:

core/todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ X sort only uses the sign of the value
1010
X more accurate than float, especially when using double and long values
1111
X consistently implement write(PrintWriter) in the List and Dict classes
1212
X add save(File) to the List and Dict classes
13+
X prevent Table.sort() from throwing NPE with empty cells
1314
_ Dict.remove() should return value, not index (for consistency w/ others)
1415
_ check again whether element 0,0 in a Table is working
1516
_ https://github.com/processing/processing/issues/3011

0 commit comments

Comments
 (0)