{"record":{"id":"c87670e6c3fb6447","repo":"dotnet/machinelearning","slug":"nullfieldwidths","errorCode":null,"errorMessage":"NullFieldWidths","messagePattern":"NullFieldWidths","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/TextFieldParser.cs","lineNumber":947,"sourceCode":"            }\n        }\n\n        private void ValidateFixedWidthLine(StringInfo line, long lineNumber)\n        {\n            Debug.Assert(line != null, \"No Line sent\");\n            if (line.LengthInTextElements < _lineLength)\n            {\n                _errorLine = line.String;\n                _errorLineNumber = checked(_lineNumber - 1);\n                throw new Exception(string.Format(Strings.CannotParseWithFieldWidths, lineNumber));\n            }\n        }\n\n        private void ValidateFieldWidths()\n        {\n            if (_fieldWidths == null)\n            {\n                throw new InvalidOperationException(Strings.NullFieldWidths);\n            }\n            if (_fieldWidths.Length == 0)\n            {\n                throw new InvalidOperationException(Strings.EmptyFieldWidths);\n            }\n            checked\n            {\n                int widthBound = _fieldWidths.Length - 1;\n                _lineLength = 0;\n                int num = widthBound - 1;\n                for (int i = 0; i <= num; i++)\n                {\n                    Debug.Assert(_fieldWidths[i] > 0, \"Bad field width, this should have been caught on input\");\n                    _lineLength += _fieldWidths[i];\n                }\n                if (_fieldWidths[widthBound] > 0)\n                {\n                    _lineLength += _fieldWidths[widthBound];","sourceCodeStart":929,"sourceCodeEnd":965,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/TextFieldParser.cs#L929-L965","documentation":"TextFieldParser.ValidateFieldWidths throws InvalidOperationException(\"NullFieldWidths\") when ReadFields is called in FixedWidth mode but the _fieldWidths array was never set. Fixed-width parsing requires the field widths to be configured beforehand via SetFieldWidths or the FieldWidths property.","triggerScenarios":"Setting TextFieldType = FieldType.FixedWidth but never calling SetFieldWidths / setting FieldWidths, then calling ReadFields. Also triggered by passing a null array to the setter paths that route into ValidateFieldWidths.","commonSituations":"Copy-pasting a delimited-file parser setup and switching TextFieldType to FixedWidth without adding widths; a code path that conditionally sets widths but the condition didn't fire; widths cleared by resetting the parser config.","solutions":["Call parser.SetFieldWidths(new[] { 5, 10, 15 }) before ReadFields when using FixedWidth","Or set TextFieldType = FieldType.Delimited and provide Delimiters instead if your data is actually delimited","Verify the code path that assigns FieldWidths actually executes before reading"],"exampleFix":"// before\nparser.TextFieldType = FieldType.FixedWidth;\nvar fields = parser.ReadFields(); // InvalidOperationException: NullFieldWidths\n// after\nparser.TextFieldType = FieldType.FixedWidth;\nparser.SetFieldWidths(new[] { 10, 20, 15 });\nvar fields = parser.ReadFields();","handlingStrategy":"validation","validationCode":"if (parser.TextFieldType == FieldType.FixedWidth && (parser.FieldWidths == null || parser.FieldWidths.Count == 0)) throw new InvalidOperationException(\"FieldWidths must be set before ReadFields in FixedWidth mode\");","typeGuard":null,"tryCatchPattern":"try { fields = parser.ReadFields(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"NullFieldWidths\") || ex.Message.Contains(\"EmptyFieldWidths\")) { /* configure widths then retry */ }","preventionTips":["Always call SetFieldWidths immediately after setting FieldType.FixedWidth","Validate parser configuration before entering the read loop","Use Delimited mode with Delimiters if you don't actually have fixed-width data"],"tags":["textfieldparser","invalidoperationexception","fixed-width","configuration"],"backgroundTag":"missing-required-config-field","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"}