diff --git a/core/src/processing/data/DoubleList.java b/core/src/processing/data/DoubleList.java index f33f024cc..e9919b3e1 100644 --- a/core/src/processing/data/DoubleList.java +++ b/core/src/processing/data/DoubleList.java @@ -681,6 +681,9 @@ public class DoubleList implements Iterable { * Return a random value from the list. */ public double random() { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList"); + } return data[(int) (Math.random() * count)]; } @@ -690,6 +693,9 @@ public class DoubleList implements Iterable { * randomSeed() from the specified sketch object. */ public double random(PApplet sketch) { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this DoubleList"); + } return data[(int) sketch.random(count)]; } diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 670b52a16..68a4703fd 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -703,6 +703,9 @@ public class FloatList implements Iterable { * Return a random value from the list. */ public float random() { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this FloatList"); + } return data[(int) (Math.random() * count)]; } @@ -712,6 +715,9 @@ public class FloatList implements Iterable { * randomSeed() from the specified sketch object. */ public float random(PApplet sketch) { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this FloatList"); + } return data[(int) sketch.random(count)]; } diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 6ef0b3f8b..d656a9419 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -678,6 +678,9 @@ public class IntList implements Iterable { * Return a random value from the list. */ public int random() { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this IntList"); + } return data[(int) (Math.random() * count)]; } @@ -687,6 +690,9 @@ public class IntList implements Iterable { * randomSeed() from the specified sketch object. */ public int random(PApplet sketch) { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this IntList"); + } return data[(int) sketch.random(count)]; } diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index f7b6e6b4d..bedbf723f 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -581,6 +581,9 @@ public class StringList implements Iterable { * Return a random value from the list. */ public String random() { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this StringList"); + } return data[(int) (Math.random() * count)]; } @@ -590,6 +593,9 @@ public class StringList implements Iterable { * randomSeed() from the specified sketch object. */ public String random(PApplet sketch) { + if (count == 0) { + throw new ArrayIndexOutOfBoundsException("No entries in this StringList"); + } return data[(int) sketch.random(count)]; } diff --git a/core/todo.txt b/core/todo.txt index 37cc53755..fa0605fde 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,5 +1,8 @@ 1287 (4.0.2) X Updating PApplet to use Java 17 (switch statements, etc) +X XxxList bug with random() if there are zero elements in the array +X now throws ArrayIndexOutOfBoundsException +X kinda want to return null, but later getting an NPE is more confusing _ concurrent StringDict et al