dotnet/machinelearning · error · InvalidOperationException

EmptyFieldWidths

Error message

EmptyFieldWidths

What it means

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.

Source

Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:951

        {
            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

  1. Call SetFieldWidths with at least one positive integer width before parsing fixed-width data.
  2. If widths come from configuration, validate the source collection is non-empty and fail fast with a clear message before constructing the parser.
  3. If the data is actually delimited rather than fixed-width, set TextFieldType to Delimited and set Delimiters instead of field widths.
  4. Guard the parser: check widths != null && widths.Length > 0 before calling SetFieldWidths.

Example fix

// before
parser.TextFieldType = FieldType.FixedWidth;
parser.SetFieldWidths(new int[] {}); // throws EmptyFieldWidths on parse
// after
int[] widths = LoadWidthsFromConfig();
if (widths == null || widths.Length == 0)
    throw new ArgumentException("At least one field width is required for fixed-width parsing.");
parser.TextFieldType = FieldType.FixedWidth;
parser.SetFieldWidths(widths);
Defensive patterns

Strategy: validation

Validate before calling

if (widths == null || widths.Length == 0)
    throw new ArgumentException("Fixed-width parsing requires at least one field width.");
parser.SetFieldWidths(widths);

Type guard

static bool HasFieldWidths(int[] widths) => widths != null && widths.Length > 0;

Try / catch

try
{
    parser.SetFieldWidths(widths);
}
catch (InvalidOperationException ex) when (ex.Message == Strings.EmptyFieldWidths)
{
    logger.LogError(ex, "Field widths not configured for fixed-width parser.");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/99331d3710cd0637. Report an issue: GitHub.