{"record":{"id":"f04435af784258b1","repo":"mrdoob/three.js","slug":"three-vector2-index-is-out-of-range-index","errorCode":null,"errorMessage":"THREE.Vector2: index is out of range: ${index}","messagePattern":"THREE\\.Vector2: index is out of range: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math/Vector2.js","lineNumber":173,"sourceCode":"\n\t\treturn this;\n\n\t}\n\n\t/**\n\t * Allows to set a vector component with an index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y.\n\t * @param {number} value - The value to set.\n\t * @return {Vector2} A reference to this vector.\n\t */\n\tsetComponent( index, value ) {\n\n\t\tswitch ( index ) {\n\n\t\t\tcase 0: this.x = value; break;\n\t\t\tcase 1: this.y = value; break;\n\t\t\tdefault: throw new Error( 'THREE.Vector2: index is out of range: ' + index );\n\n\t\t}\n\n\t\treturn this;\n\n\t}\n\n\t/**\n\t * Returns the value of the vector component which matches the given index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y.\n\t * @return {number} A vector component value.\n\t */\n\tgetComponent( index ) {\n\n\t\tswitch ( index ) {\n\n\t\t\tcase 0: return this.x;","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/src/math/Vector2.js#L155-L191","documentation":"Thrown by Vector2.setComponent(index, value) when index is not 0 or 1. setComponent maps index 0 to x and 1 to y for generic/indexed access; a Vector2 has only two components, so any other index is out of range.","triggerScenarios":"Calling vec2.setComponent(2, v) or any index outside [0,1]. Looping over a component count > 2. Passing an index derived from a Vector3/Vector4 component count into a Vector2. Negative or NaN indices.","commonSituations":"Generic math code that loops dim times and feeds the same index to vectors of different sizes. Deserialization feeding a 3- or 4-component tuple into a Vector2. Off-by-one in index calculations.","solutions":["Restrict the index to 0 or 1 for Vector2; clamp or branch before calling setComponent.","Use the correct vector type for your component count (Vector3 for 3, Vector4 for 4).","When looping, drive the bound from the target vector's actual size, not a shared constant.","Access components directly (vec.x, vec.y) when the index is known at write time."],"exampleFix":"// before: assuming 3 components\nfor (let i = 0; i < 3; i++) v2.setComponent(i, data[i]); // i===2 throws\n\n// after: bound to Vector2 size, or use the right type\nfor (let i = 0; i < 2; i++) v2.setComponent(i, data[i]);","handlingStrategy":"validation","validationCode":"function setComponentSafe(vec, index, value) {\n  if (index < 0 || index > 1) {\n    throw new RangeError(`Vector2 index must be 0 or 1, got ${index}`);\n  }\n  vec.setComponent(index, value);\n  return vec;\n}","typeGuard":"function isValidVector2Index(index) {\n  return Number.isInteger(index) && index >= 0 && index <= 1;\n}","tryCatchPattern":null,"preventionTips":["Drive indexed loops from the target vector's size (2 for Vector2).","Use Vector3/Vector4 when more components are needed.","Prefer direct field access (vec.x, vec.y) for known components."],"tags":["threejs","vector","vector2","index","bounds"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}