{"record":{"id":"99331d3710cd0637","repo":"dotnet/machinelearning","slug":"emptyfieldwidths","errorCode":null,"errorMessage":"EmptyFieldWidths","messagePattern":"EmptyFieldWidths","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/TextFieldParser.cs","lineNumber":951,"sourceCode":"        {\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];\n                }\n            }\n        }\n","sourceCodeStart":933,"sourceCodeEnd":969,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/TextFieldParser.cs#L933-L969","documentation":"TextFieldParser.SetFieldWidths (fixed-width mode) throws InvalidOperationException with message EmptyFieldWidths when the parser's field widths array is empty. Fixed-width parsing requires at least one positive width per field; an empty array provides no layout to split lines with. The library throws this eagerly when parsing is attempted so the caller knows the widths configuration is invalid before any line is read.","triggerScenarios":"Calling SetFieldWidths(new int[0]) (or an equivalent empty array) and then reading fields, or calling ReadFields/PeekChars on a TextFieldParser configured with TextFieldType.FixedWidth whose field widths array has length 0.","commonSituations":"Building field widths programmatically from an empty config source (database table, JSON list) that returned no rows; passing a collection that was filtered down to nothing; a refactor that accidentally clears the widths array before parsing.","solutions":["Call SetFieldWidths with at least one positive integer width before parsing fixed-width data.","If widths come from configuration, validate the source collection is non-empty and fail fast with a clear message before constructing the parser.","If the data is actually delimited rather than fixed-width, set TextFieldType to Delimited and set Delimiters instead of field widths.","Guard the parser: check widths != null && widths.Length > 0 before calling SetFieldWidths."],"exampleFix":"// before\nparser.TextFieldType = FieldType.FixedWidth;\nparser.SetFieldWidths(new int[] {}); // throws EmptyFieldWidths on parse\n// after\nint[] widths = LoadWidthsFromConfig();\nif (widths == null || widths.Length == 0)\n    throw new ArgumentException(\"At least one field width is required for fixed-width parsing.\");\nparser.TextFieldType = FieldType.FixedWidth;\nparser.SetFieldWidths(widths);","handlingStrategy":"validation","validationCode":"if (widths == null || widths.Length == 0)\n    throw new ArgumentException(\"Fixed-width parsing requires at least one field width.\");\nparser.SetFieldWidths(widths);","typeGuard":"static bool HasFieldWidths(int[] widths) => widths != null && widths.Length > 0;","tryCatchPattern":"try\n{\n    parser.SetFieldWidths(widths);\n}\ncatch (InvalidOperationException ex) when (ex.Message == Strings.EmptyFieldWidths)\n{\n    logger.LogError(ex, \"Field widths not configured for fixed-width parser.\");\n}","preventionTips":["Validate configuration collections are non-empty before wiring the parser","Decide delimited vs fixed-width mode up front so you never set widths in delimited mode","Unit-test width loading from its config source with empty-input cases"],"tags":["fixed-width-parsing","empty-array","configuration","microsoft-data-analysis"],"backgroundTag":"empty-required-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"}