dotnet/machinelearning · error · InvalidOperationException
NullFieldWidths
Error message
NullFieldWidths
What it means
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.
Source
Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:947
}
}
private void ValidateFixedWidthLine(StringInfo line, long lineNumber)
{
Debug.Assert(line != null, "No Line sent");
if (line.LengthInTextElements < _lineLength)
{
_errorLine = line.String;
_errorLineNumber = checked(_lineNumber - 1);
throw new Exception(string.Format(Strings.CannotParseWithFieldWidths, lineNumber));
}
}
private void ValidateFieldWidths()
{
if (_fieldWidths == null)
{
throw new InvalidOperationException(Strings.NullFieldWidths);
}
if (_fieldWidths.Length == 0)
{
throw new InvalidOperationException(Strings.EmptyFieldWidths);
}
checked
{
int widthBound = _fieldWidths.Length - 1;
_lineLength = 0;
int num = widthBound - 1;
for (int i = 0; i <= num; i++)
{
Debug.Assert(_fieldWidths[i] > 0, "Bad field width, this should have been caught on input");
_lineLength += _fieldWidths[i];
}
if (_fieldWidths[widthBound] > 0)
{
_lineLength += _fieldWidths[widthBound];View on GitHub (pinned to 7b76e69cf9)
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
Example fix
// before
parser.TextFieldType = FieldType.FixedWidth;
var fields = parser.ReadFields(); // InvalidOperationException: NullFieldWidths
// after
parser.TextFieldType = FieldType.FixedWidth;
parser.SetFieldWidths(new[] { 10, 20, 15 });
var fields = parser.ReadFields(); Defensive patterns
Strategy: validation
Validate before calling
if (parser.TextFieldType == FieldType.FixedWidth && (parser.FieldWidths == null || parser.FieldWidths.Count == 0)) throw new InvalidOperationException("FieldWidths must be set before ReadFields in FixedWidth mode"); Try / catch
try { fields = parser.ReadFields(); } catch (InvalidOperationException ex) when (ex.Message.Contains("NullFieldWidths") || ex.Message.Contains("EmptyFieldWidths")) { /* configure widths then retry */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.
Related errors
- PositiveNumberOfCharacters
- StreamDoesntSupportReading
- FileNotFound
- EmptyFieldWidths
- You need to configure a default, not named, model before you
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/c87670e6c3fb6447.
Report an issue: GitHub.