egametang/ET · error · NullReferenceException
如果项非string类型,则必需设置解析器
Error message
如果项非string类型,则必需设置解析器
What it means
StrConv.ToArr<T> splits a string into an array of T. When T is not string, the caller must supply a parse delegate (Func<string,T>) to convert each segment. If parse is null for a non-string T, a NullReferenceException is thrown with this message. This is a developer-contract violation, not a runtime data error.
Source
Thrown at Packages/cn.etetet.packagemanager/Editor/Tools/StrConv.cs:333
{
if (string.IsNullOrEmpty(valueIn))
{
valueOut = new T[0];
return true;
}
string[] strValueArr = valueIn.Split(new[] { separator }, StringSplitOptions.None);
Type tType = typeof(T);
if (typeof(string) == tType)
{
valueOut = strValueArr as T[];
return true;
}
if (parse == null)
{
throw new NullReferenceException("如果项非string类型,则必需设置解析器");
}
valueOut = new T[strValueArr.Length];
for (int i = 0; i < strValueArr.Length; i++)
{
valueOut[i] = parse(strValueArr[i]);
}
return true;
}
/// <summary>
/// 通过一定规则,把字答串数组转为对应类型数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values"></param>
/// <param name="parse"></param>
/// <returns></returns>View on GitHub (pinned to 5cab01f7a8)
Solutions
- Pass a valid parse delegate, e.g. ToArr(valueIn, sep, out arr, int.Parse).
- If T is genuinely string, ensure the generic argument is string so the early-return path is taken.
- Add a compile-time/unit test that ToArr is never called with null parse for value types.
Example fix
// before StrUtil.ToArr<int>(raw, ",", out int[] vals, null); // after StrUtil.ToArr<int>(raw, ",", out int[] vals, int.Parse);
Defensive patterns
Strategy: validation
Validate before calling
if (typeof(T) != typeof(string) && parse == null) { throw new ArgumentException("parse is required for non-string T"); } Prevention
- Always pass a parser for non-string T when calling ToArr.
- For string arrays, rely on the string fast path (no parser needed).
- Add a unit test asserting ToArr throws clearly when parse is null.
When it happens
Trigger: Calling ToArr<int>(valueIn, sep, out arr, null) or any non-string T with a null parser argument; a refactor changed T from string to another type but forgot to pass the parser.
Common situations: Configuration/table import code that parses typed arrays but the parser lambda was omitted; copy-paste of a ToArr call where the parse argument was dropped.
Related errors
- text={3}, index={0}, chr='{1}':此处只能出现'{2}'
- text={2}, index={0}, chr='{1}', 此处只能出现小写英文字符
- text={2}, index={0}, chr='{1}', 此处只能出现小写英文或1-9的数字
- text={2}, index={0}, chr='{1}', 0-9的数字或'}}'
- 没有正确结束, state={state}
AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13).
Data as JSON: /api/errors/b7e692d13d4c8d51.
Report an issue: GitHub.