{"record":{"id":"0879634fdbb0bb4f","repo":"apache/flink","slug":"length-must-be-between-0-and-the-current-length","errorCode":null,"errorMessage":"Length must be between 0 and the current length.","messagePattern":"Length must be between 0 and the current length\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/types/StringValue.java","lineNumber":132,"sourceCode":"     * @param len The length of the substring.\n     */\n    public StringValue(StringValue value, int offset, int len) {\n        this.value = EMPTY_STRING;\n        setValue(value, offset, len);\n    }\n\n    // --------------------------------------------------------------------------------------------\n    //                                Getters and Setters\n    // --------------------------------------------------------------------------------------------\n\n    /**\n     * Sets a new length for the string.\n     *\n     * @param len The new length.\n     */\n    public void setLength(int len) {\n        if (len < 0 || len > this.len) {\n            throw new IllegalArgumentException(\"Length must be between 0 and the current length.\");\n        }\n        this.len = len;\n    }\n\n    /**\n     * Returns this StringValue's internal character data. The array might be larger than the string\n     * which is currently stored in the StringValue.\n     *\n     * @return The character data.\n     */\n    public char[] getCharArray() {\n        return this.value;\n    }\n\n    /**\n     * Gets this StringValue as a String.\n     *\n     * @return A String resembling the contents of this StringValue.","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/types/StringValue.java#L114-L150","documentation":"StringValue is a mutable char-array-backed string. setLength(int len) only truncates: it can shrink the logical length but never grow it, because the backing array is unchanged and the invariant is 0 <= len <= this.len. Any attempt to set a larger (or negative) length throws IllegalArgumentException.","triggerScenarios":"Calling stringValue.setLength(n) with n negative or n greater than the string's current length(); e.g. trying to pre-size or 'extend' a StringValue the way StringBuilder.setLength does.","commonSituations":"Developers transferring StringBuilder habits (where setLength can grow) to StringValue; reusing a StringValue and trying to reset it to a capacity rather than a logical length.","solutions":["To truncate only: pass 0 <= len <= current length()","To change content/size arbitrarily, call setValue(...) with the new content instead","Read current bounds with length() before calling setLength"],"exampleFix":"// before\nsv.setLength(sv.length() + 10); // throws\n\n// after\nsv.setValue(\"new longer content\");","handlingStrategy":"validation","validationCode":"if (len < 0 || len > sv.length()) {\n    throw new IllegalArgumentException(\"len must be in [0, \" + sv.length() + \"]\");\n}\nsv.setLength(len);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["setLength only truncates — use setValue to grow","Read length() first","Don't transfer StringBuilder habits"],"tags":["flink","string-value","illegal-argument","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}