TheAlgorithms/Java · error · IllegalArgumentException
Keys and frequencies cannot be null
Error message
Keys and frequencies cannot be null
What it means
Thrown by OptimalBinarySearchTree (validateInput) when either the keys array or the frequencies array is null. The algorithm pairs keys with frequencies to build an OBST; a null array breaks the length check and indexing. Message: 'Keys and frequencies cannot be null'.
Source
Thrown at src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java:89
for (int currentRoot = leftBoundary; currentRoot <= rightBoundary; currentRoot++) {
long leftCost = currentRoot > start ? optimalCost[start][currentRoot - 1] : 0L;
long rightCost = currentRoot < end ? optimalCost[currentRoot + 1][end] : 0L;
long currentCost = frequencySum + leftCost + rightCost;
if (currentCost < optimalCost[start][end]) {
optimalCost[start][end] = currentCost;
root[start][end] = currentRoot;
}
}
}
}
return optimalCost[0][nodeCount - 1];
}
private static void validateInput(int[] keys, int[] frequencies) {
if (keys == null || frequencies == null) {
throw new IllegalArgumentException("Keys and frequencies cannot be null");
}
if (keys.length != frequencies.length) {
throw new IllegalArgumentException("Keys and frequencies must have the same length");
}
for (int frequency : frequencies) {
if (frequency < 0) {
throw new IllegalArgumentException("Frequencies cannot be negative");
}
}
}
private static int[][] sortNodes(int[] keys, int[] frequencies) {
int[][] sortedNodes = new int[keys.length][2];
for (int index = 0; index < keys.length; index++) {
sortedNodes[index][0] = keys[index];
sortedNodes[index][1] = frequencies[index];
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure both keys and frequencies arrays are constructed and populated before the call.
- Guard both arrays with Objects.requireNonNull at the boundary where they are produced.
- If input is optional, return early or use a sentinel (e.g. empty arrays) consistent with the algorithm's semantics.
Example fix
// before int cost = OptimalBinarySearchTree.optimize(keys, freq); // after Objects.requireNonNull(keys, "keys"); Objects.requireNonNull(freq, "freq"); int cost = OptimalBinarySearchTree.optimize(keys, freq);
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(keys, "keys"); Objects.requireNonNull(frequencies, "frequencies"); // then call optimize(keys, frequencies)
Type guard
keys != null && frequencies != null
Prevention
- Build keys and frequencies together from one source so neither is null.
- Use Objects.requireNonNull at construction, not just before the call.
- Prefer records/DTOs that enforce non-null arrays.
When it happens
Trigger: Passing null for keys or frequencies; one array constructed and the other left null due to a branching bug; deserialization that produced null arrays for absent fields.
Common situations: Config-driven builds where a list was omitted; refactoring that removed one array's population but not its declaration.
Related errors
- Price array cannot be null or empty.
- Input array cannot be null or empty.
- Input string must not be null
- Input array should not contain negative number(s).
- Keys and frequencies must have the same length
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/531e4a8b9c40c8d8.
Report an issue: GitHub.