{"record":{"id":"cc666ee3000d67da","repo":"dotnet/machinelearning","slug":"specified-argument-was-out-of-the-range-of-valid-v-cc666e","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'columnIndex')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'columnIndex'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.DataView/DataViewSchema.cs","lineNumber":53,"sourceCode":"        {\n            get\n            {\n                if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));\n                if (!_nameMap.TryGetValue(name, out int col))\n                    throw new ArgumentOutOfRangeException(nameof(name), $\"Column '{name}' not found\");\n                return _columns[col];\n            }\n        }\n\n        /// <summary>\n        /// Get the column by index.\n        /// </summary>\n        public Column this[int columnIndex]\n        {\n            get\n            {\n                if (!(0 <= columnIndex && columnIndex < _columns.Length))\n                    throw new ArgumentOutOfRangeException(nameof(columnIndex));\n                return _columns[columnIndex];\n            }\n        }\n\n        /// <summary>\n        /// Get the column by name, or <c>null</c> if the column is not present.\n        /// </summary>\n        public Column? GetColumnOrNull(string name)\n        {\n            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));\n            if (_nameMap.TryGetValue(name, out int col))\n                return _columns[col];\n            return null;\n        }\n\n        public IEnumerator<Column> GetEnumerator() => ((IEnumerable<Column>)_columns).GetEnumerator();\n\n        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.DataView/DataViewSchema.cs#L35-L71","documentation":"DataViewSchema's indexer validates that columnIndex falls within the schema's column range before returning the Column. If the index is negative or >= the number of columns, an ArgumentOutOfRangeException naming 'columnIndex' is thrown. This guards against callers relying on stale or external indices that no longer match the schema.","triggerScenarios":"Accessing schema[columnIndex] with a negative index, an index equal to or greater than schema.Count/_columns.Length, or an index captured from a different (older or other) schema whose column count shrank.","commonSituations":"Reusing column indices saved from a previous pipeline fit; iterating with an off-by-one loop (i <= Count); a transform dropped columns so an index from the input schema is invalid on the output schema.","solutions":["Check the index against schema.Count before indexing: if (index >= 0 && index < schema.Count) { ... }.","Instead of indices, look the column up by name with GetColumnOrNull(name) and use its Index property on the current schema.","Recompute column indices from the schema you are actually using rather than caching them across transforms.","Fix loop bounds to i < schema.Count (not <=)."],"exampleFix":"// before\nvar col = schema[columnIndex]; // throws when columnIndex out of range\n// after\nvar col = columnIndex >= 0 && columnIndex < schema.Count ? schema[columnIndex] : null;\nif (col == null) { /* handle missing column */ }","handlingStrategy":"validation","validationCode":"bool isValidIndex = index >= 0 && index < schema.Count;","typeGuard":"bool HasColumn(DataViewSchema schema, int index) => schema != null && index >= 0 && index < schema.Count;","tryCatchPattern":null,"preventionTips":["Always bound-check indices against schema.Count before indexing","Look columns up by name instead of caching numeric indices across transforms","Use the schema object you are actually reading, not the input schema"],"tags":["dotnet","argument-out-of-range","schema","index"],"backgroundTag":"index-out-of-range","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}