babalae/better-genshin-impact · error · ArgumentException

候选文本不能包含空字符串或纯空白字符串

Error message

候选文本不能包含空字符串或纯空白字符串

What it means

BvPage.GetByAnyText builds an OCR locator that matches any one of several candidate texts; it throws ArgumentException if any candidate is null, empty, or whitespace. The match set is later consumed by the OCR recognition pipeline where an empty string would either match nothing or spuriously match blank regions, so the guard validates the whole collection up front after ParseCollection has coerced it from a JS array.

Source

Thrown at BetterGenshinImpact/Core/BgiVision/BvPage.cs:122

    /// </summary>
    /// <param name="ro"></param>
    /// <returns></returns>
    public BvLocator Locator(RecognitionObject ro)
    {
        return new BvLocator(ro, _cancellationToken);
    }

    public BvLocator GetByText(string text = "", Rect rect = default)
    {
        return Locator(text, rect);
    }

    public BvLocator GetByAnyText(object texts, Rect rect = default)
    {
        var matchTexts = ParseCollection<string>(texts, nameof(texts));
        if (matchTexts.Any(string.IsNullOrWhiteSpace))
        {
            throw new ArgumentException("候选文本不能包含空字符串或纯空白字符串", nameof(texts));
        }

        matchTexts = matchTexts.Distinct(StringComparer.Ordinal).ToList();
        return new BvLocator(new RecognitionObject
        {
            RecognitionType = RecognitionTypes.Ocr,
            RegionOfInterest = rect
        }, _cancellationToken, matchTexts);
    }

    public BvLocator GetByImage(BvImage image)
    {
        return Locator(image);
    }


    public List<Region> Ocr(Rect rect = default)
    {

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Filter the candidate list before passing it: texts.Where(s => !string.IsNullOrWhiteSpace(s)).
  2. Validate upstream that every key in your text table resolves to a non-blank value.
  3. When building from config, skip null/blank entries at load time.

Example fix

// before
var loc = page.GetByAnyText(new[] { "确认", "", "确定" });

// after
var candidates = new[] { "确认", "", "确定" }
    .Where(s => !string.IsNullOrWhiteSpace(s))
    .ToArray();
var loc = page.GetByAnyText(candidates);
Defensive patterns

Strategy: validation

Validate before calling

var clean = candidates
    .Where(s => !string.IsNullOrWhiteSpace(s))
    .Distinct(StringComparer.Ordinal)
    .ToList();
if (clean.Count > 0)
    var loc = page.GetByAnyText(clean);

Type guard

static bool AllNonBlank(IEnumerable<string> xs) => xs.All(s => !string.IsNullOrWhiteSpace(s));

Prevention

When it happens

Trigger: Calling GetByAnyText(new[]{ "确认", "", "确定" }) ; passing a JS array like ["OK", null, "YES"]; a candidate string that is only spaces.

Common situations: Lists assembled dynamically where one entry was never set; localized text tables with missing keys rendered as empty; trimming user input that leaves blank entries.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/021564e2fdd1b337. Report an issue: GitHub.