babalae/better-genshin-impact · error · ArgumentException

参数ItemName和ItemNames不能同时使用

Error message

参数ItemName和ItemNames不能同时使用

What it means

Thrown as ArgumentException by CountInventoryItemParam.Validate when both ItemName (single) and ItemNames (list) are non-empty simultaneously. The task accepts exactly one form of item specification to avoid ambiguity in which items to count; providing both is treated as a caller bug.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Job/CountInventoryItemParam.cs:45

    public IEnumerable<string>? GetItemNamesOrNull()
    {
        return ItemNames.Count > 0 ? ItemNames : null;
    }

    public void Validate()
    {
        ItemNames ??= [];
        bool hasItemName = !string.IsNullOrWhiteSpace(ItemName);
        bool hasItemNames = ItemNames.Count > 0;
        if (!hasItemName)
        {
            ItemName = null;
        }

        if (hasItemName && hasItemNames)
        {
            throw new ArgumentException($"参数{nameof(ItemName)}和{nameof(ItemNames)}不能同时使用");
        }

        if (!hasItemName && !hasItemNames)
        {
            throw new ArgumentException($"参数{nameof(ItemName)}和{nameof(ItemNames)}不能同时为空");
        }

        if (ItemNames.Any(string.IsNullOrWhiteSpace))
        {
            throw new ArgumentException($"参数{nameof(ItemNames)}不能包含空名称");
        }
    }
}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Set only ItemName for a single item, or only ItemNames for multiple — never both.
  2. Before calling Validate(), explicitly null out the field you are not using.
  3. Review the code that constructs CountInventoryItemParam to ensure it picks one branch.

Example fix

// before
param.ItemName = "树脂";
param.ItemNames = new List<string> { "树脂", "原石" };
param.Validate(); // throws

// after
param.ItemName = null;
param.ItemNames = new List<string> { "树脂", "原石" };
param.Validate();
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrWhiteSpace(param.ItemName) && param.ItemNames.Count > 0)
{
    // pick one: clear ItemNames if using single name
    param.ItemNames.Clear();
}

Type guard

static bool IsParamExclusive(CountInventoryItemParam p)
{
    bool hasName = !string.IsNullOrWhiteSpace(p.ItemName);
    bool hasList = p.ItemNames.Count > 0;
    return hasName ^ hasList;
}

Try / catch

try { param.Validate(); }
catch (ArgumentException ex) when (ex.Message.Contains("不能同时使用")) { /* clear one field, retry */ }

Prevention

When it happens

Trigger: Constructing a CountInventoryItemParam and setting both ItemName = "someItem" and ItemNames = ["item1", "item2"], then calling Validate().

Common situations: A script or UI builder populates both fields from different code paths and forgets to clear one. Copy-paste from another param object leaves a stale ItemName alongside a new ItemNames list.

Related errors


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