stanfordnlp/CoreNLP · error · IllegalArgumentException
Array must be sorted!
Error message
Array must be sorted!
What it means
ArrayUtils.gapEncodeList computes gap encoding of an int array, which is only well-defined for monotonically non-decreasing sequences (each gap must be non-negative). It scans the array and throws IllegalArgumentException('Array must be sorted!') if any element is smaller than its predecessor.
Solutions
- Sort the input array first with Arrays.sort(orig) before calling gapEncodeList.
- If you must preserve the original order, sort a copy: int[] sorted = orig.clone(); Arrays.sort(sorted); gapEncodeList(sorted).
- Pre-validate with a loop or IntStream check that no element is smaller than the previous one, and handle the unsorted case explicitly.
Example fix
// before List<Byte> encoded = ArrayUtils.gapEncodeList(docIds); // docIds unsorted // after int[] sorted = docIds.clone(); Arrays.sort(sorted); List<Byte> encoded = ArrayUtils.gapEncodeList(sorted);
Defensive patterns
Strategy: validation
Validate before calling
for (int i = 1; i < orig.length; i++) {
if (orig[i] < orig[i-1]) throw new IllegalArgumentException("input to gapEncodeList must be sorted");
} Try / catch
try {
List<Byte> out = ArrayUtils.gapEncodeList(orig);
} catch (IllegalArgumentException e) {
int[] sorted = orig.clone(); Arrays.sort(sorted);
List<Byte> out = ArrayUtils.gapEncodeList(sorted);
} Prevention
- Sort arrays (Arrays.sort) immediately before any gap/delta encoding.
- Never iterate a HashMap to produce positions for encoding; use sorted structures (TreeMap) or sort first.
- Add a unit test asserting monotonicity for encoder inputs.
When it happens
Trigger: Calling ArrayUtils.gapEncodeList(int[] orig) where for some i>0, orig[i] < orig[i-1], i.e. the array contains a decreasing pair; also triggered transitively via encodedList on unsorted input.
Common situations: Passing positions/docIDs that were not sorted because they came from a HashMap iteration order; appending new items to an encoded list without re-sorting; assuming gapEncodeList sorts for you (it validates but does not sort).
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Arrays do not have the same length.
- Can only operate on preterminals
- CoreMap must have either a Calendar or DocDate annotation
- dim should be an array of size 2.
- Neither element of pair comparable
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/0cefd541496f461c.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/ArrayUtils.java:40
private static final Redwood.RedwoodChannels log = Redwood.channels(ArrayUtils.class);
/**
* Should not be instantiated
*/
private ArrayUtils() {}
public static byte[] gapEncode(int[] orig) {
List<Byte> encodedList = gapEncodeList(orig);
byte[] arr = new byte[encodedList.size()];
int i = 0;
for (byte b : encodedList) { arr[i++] = b; }
return arr;
}
public static List<Byte> gapEncodeList(int[] orig) {
for (int i = 1; i < orig.length; i++) {
if (orig[i] < orig[i-1]) {
throw new IllegalArgumentException("Array must be sorted!");
}
}
List<Byte> bytes = new ArrayList<>();
int index = 0;
int prevNum = 0;
byte currByte = 0 << 8;
for (int f : orig) {
String n = (f == prevNum ? "" : Integer.toString(f-prevNum, 2));
for (int ii = 0; ii < n.length(); ii++) {
if (index == 8) {
bytes.add(currByte);
currByte = 0 << 8;
index = 0;
}
currByte <<= 1;View on GitHub (pinned to 1b7edd19c4)