dotnet/machinelearning · error · ArgumentException
PositiveNumberOfCharacters
Error message
PositiveNumberOfCharacters
What it means
TextFieldParser.PeekChars throws ArgumentException("PositiveNumberOfCharacters") when numberOfChars is zero or negative. PeekChars only makes sense for a positive count of characters to inspect in the next line, so non-positive arguments are rejected up front.
Source
Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:453
return ParseFixedWidthLine();
case FieldType.Delimited:
return ParseDelimitedLine();
default:
Debug.Fail("The TextFieldType is not supported");
return null;
}
}
///<summary>
/// Peek at <paramref name="numberOfChars"/> characters of the next data line without reading the line
///</summary>
///<param name="numberOfChars">The number of characters to look at in the next data line.</param>
///<returns>A string consisting of the first <paramref name="numberOfChars"/> characters of the next line. >If numberOfChars is greater than the next line, only the next line is returned</returns>
public string PeekChars(int numberOfChars)
{
if (numberOfChars <= 0)
{
throw new ArgumentException(string.Format(Strings.PositiveNumberOfCharacters, nameof(numberOfChars)));
}
if ((_reader == null) || (_buffer == null))
{
return null;
}
if (_endOfData)
{
return null;
}
string line = PeekNextDataLine();
if (line == null)
{
_endOfData = true;
return null;
}View on GitHub (pinned to 7b76e69cf9)
Solutions
- Pass a strictly positive integer to PeekChars (numberOfChars >= 1)
- Guard the call site: only call PeekChars when the computed count is > 0
- Fix the source of the non-positive value (config default, counter, calculation)
Example fix
// before parser.PeekChars(previewLength); // previewLength could be 0 // after string peeked = previewLength > 0 ? parser.PeekChars(previewLength) : null;
Defensive patterns
Strategy: validation
Validate before calling
if (numberOfChars <= 0) throw new ArgumentOutOfRangeException(nameof(numberOfChars), "PeekChars requires a positive character count.");
Try / catch
try { peeked = parser.PeekChars(n); } catch (ArgumentException ex) when (ex.Message.Contains("PositiveNumberOfCharacters")) { peeked = null; } Prevention
- Never pass literal 0 or negative values to PeekChars
- Clamp computed values: Math.Max(1, count)
- Validate config-driven preview sizes at startup
When it happens
Trigger: Calling PeekChars(0) or PeekChars with a negative int, often when the value comes from a computed variable or user configuration rather than a literal.
Common situations: A config value for 'preview length' defaulting to 0; an off-by-one in decrementing a counter before calling PeekChars; passing an uninitialized/zero int field.
Related errors
- StreamDoesntSupportReading
- Specified argument was out of the range of valid values. (Pa
- index
- Strings.IndexIsGreaterThanColumnLength
- string.Format(Strings.MismatchedValueType, typeof(string))
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/7f8662009e55dd3b.
Report an issue: GitHub.