{"record":{"id":"531e4a8b9c40c8d8","repo":"TheAlgorithms/Java","slug":"keys-and-frequencies-cannot-be-null","errorCode":null,"errorMessage":"Keys and frequencies cannot be null","messagePattern":"Keys and frequencies cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java","lineNumber":89,"sourceCode":"                for (int currentRoot = leftBoundary; currentRoot <= rightBoundary; currentRoot++) {\n                    long leftCost = currentRoot > start ? optimalCost[start][currentRoot - 1] : 0L;\n                    long rightCost = currentRoot < end ? optimalCost[currentRoot + 1][end] : 0L;\n                    long currentCost = frequencySum + leftCost + rightCost;\n\n                    if (currentCost < optimalCost[start][end]) {\n                        optimalCost[start][end] = currentCost;\n                        root[start][end] = currentRoot;\n                    }\n                }\n            }\n        }\n\n        return optimalCost[0][nodeCount - 1];\n    }\n\n    private static void validateInput(int[] keys, int[] frequencies) {\n        if (keys == null || frequencies == null) {\n            throw new IllegalArgumentException(\"Keys and frequencies cannot be null\");\n        }\n        if (keys.length != frequencies.length) {\n            throw new IllegalArgumentException(\"Keys and frequencies must have the same length\");\n        }\n\n        for (int frequency : frequencies) {\n            if (frequency < 0) {\n                throw new IllegalArgumentException(\"Frequencies cannot be negative\");\n            }\n        }\n    }\n\n    private static int[][] sortNodes(int[] keys, int[] frequencies) {\n        int[][] sortedNodes = new int[keys.length][2];\n        for (int index = 0; index < keys.length; index++) {\n            sortedNodes[index][0] = keys[index];\n            sortedNodes[index][1] = frequencies[index];\n        }","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java#L71-L107","documentation":"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'.","triggerScenarios":"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.","commonSituations":"Config-driven builds where a list was omitted; refactoring that removed one array's population but not its declaration.","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."],"exampleFix":"// before\nint cost = OptimalBinarySearchTree.optimize(keys, freq);\n\n// after\nObjects.requireNonNull(keys, \"keys\");\nObjects.requireNonNull(freq, \"freq\");\nint cost = OptimalBinarySearchTree.optimize(keys, freq);","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(keys, \"keys\");\nObjects.requireNonNull(frequencies, \"frequencies\");\n// then call optimize(keys, frequencies)","typeGuard":"keys != null && frequencies != null","tryCatchPattern":null,"preventionTips":["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."],"tags":["null-check","array","dynamic-programming","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}