bchavez/Bogus · error · ArgumentException
The collection is empty. There are no items to select.
Error message
The collection is empty. There are no items to select.
What it means
Thrown by Randomizer.CollectionItem<T> when collection.Count <= 0. The method uses Skip(r).First() so it needs at least one element. The param is named 'collection'.
Source
Thrown at Source/Bogus/Randomizer.cs:571
}
/// <summary>
/// Get a random subset of a List.
/// </summary>
/// <param name="items">The source of items to pick from.</param>
/// <param name="count">The number of items to pick; otherwise, a random amount is picked.</param>
public IList<T> ListItems<T>(List<T> items, int? count = null)
{
return ListItems(items as IList<T>, count);
}
/// <summary>
/// Get a random collection item.
/// </summary>
public T CollectionItem<T>(ICollection<T> collection)
{
if( collection.Count <= 0 )
throw new ArgumentException("The collection is empty. There are no items to select.", nameof(collection));
var r = Number(max: collection.Count - 1);
return collection.Skip(r).First();
}
/// <summary>
/// Replaces symbols with numbers.
/// IE: ### -> 283
/// </summary>
/// <param name="format">The string format</param>
/// <param name="symbol">The symbol to search for in format that will be replaced with a number</param>
public string ReplaceNumbers(string format, char symbol = '#')
{
return ReplaceSymbols(format, symbol, () => Convert.ToChar('0' + Number(9)));
}
/// <summary>
/// Replaces each character instance in a string.View on GitHub (pinned to 6ece18c5c2)
Solutions
- Check collection.Count > 0 before calling CollectionItem.
- Populate or supply a fallback collection.
- Guard upstream filters that can yield empty results.
Example fix
// before var v = f.Random.CollectionItem(set); // after var v = set.Count > 0 ? f.Random.CollectionItem(set) : defaultValue;
Defensive patterns
Strategy: validation
Validate before calling
static T SafeCollectionItem<T>(Bogus.Randomizer r, ICollection<T> c, T fallback) {
return c.Count > 0 ? r.CollectionItem(c) : fallback;
} Type guard
bool HasItems<T>(ICollection<T> c) => c.Count > 0;
Try / catch
try { return r.CollectionItem(c); }
catch (ArgumentException ex) when (ex.Message.Contains("collection is empty")) {
return default;
} Prevention
- Check collection.Count before sampling.
- Guard filters that can yield empty collections.
- Provide a non-empty fallback collection.
When it happens
Trigger: Calling f.Random.CollectionItem(new HashSet<T>()) or any ICollection<T> with zero elements.
Common situations: Passing an empty set/dictionary-keys collection; results of a where-filter that produced nothing.
Related errors
- The array is empty. There are no items to select.
- The list is empty. There are no items to select.
- max digit can't be lager than 9 or smaller than 0
- The min/max range is invalid. The minimum value '{min}' is g
- The specified range does not contain any even numbers.
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/226613555a6c4f68.
Report an issue: GitHub.