EllanJiang/GameFramework · error · GameFrameworkException
Encoding is invalid.
Error message
Encoding is invalid.
What it means
Utility.Converter.GetBytes(string value, Encoding encoding) throws this when the encoding parameter is null. The method simply delegates to encoding.GetBytes(value), so a null encoding would cause a NullReferenceException; the explicit GameFrameworkException gives a clearer, fail-fast diagnostic instead.
Solutions
- Pass an explicit encoding such as Encoding.UTF8 at the call site.
- If the encoding comes from config, fall back to Encoding.UTF8 when the configured value is missing/invalid.
- Fix the encoding lookup (e.g. Encoding.GetEncoding throws for bad names — handle it and use a default).
- Initialize static/shared Encoding fields in static constructors so they are never null.
Example fix
// before var bytes = Utility.Converter.GetBytes(text, configuredEncoding); // after var bytes = Utility.Converter.GetBytes(text, configuredEncoding ?? Encoding.UTF8);
Defensive patterns
Strategy: validation
Validate before calling
if (encoding == null) encoding = Encoding.UTF8;
Type guard
bool IsValidEncoding(Encoding encoding) => encoding != null;
Try / catch
try { bytes = Utility.Converter.GetBytes(text, encoding); }
catch (GameFrameworkException ex) { bytes = Utility.Converter.GetBytes(text, Encoding.UTF8); } Prevention
- Default to Encoding.UTF8 whenever an encoding is optional
- Validate encoding names from config at startup
- Initialize Encoding fields in static constructors so they are never null
When it happens
Trigger: Calling Utility.Converter.GetBytes("text", null) — typically when the encoding comes from config, a lookup by name (Encoding.GetEncoding returning null in custom wrappers), or an uninitialized static field.
Common situations: Encoding chosen from settings where the name is invalid or missing; a helper class whose Encoding field is initialized only in one constructor; cross-platform code assuming a default encoding exists.
Related errors
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/4cd9cd94c23c9d94.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Utility/Utility.Converter.cs:740
return GetBytes(value, Encoding.UTF8, buffer, startIndex);
}
/// <summary>
/// 以字节数组的形式获取指定编码的字符串。
/// </summary>
/// <param name="value">要转换的字符串。</param>
/// <param name="encoding">要使用的编码。</param>
/// <returns>用于存放结果的字节数组。</returns>
public static byte[] GetBytes(string value, Encoding encoding)
{
if (value == null)
{
throw new GameFrameworkException("Value is invalid.");
}
if (encoding == null)
{
throw new GameFrameworkException("Encoding is invalid.");
}
return encoding.GetBytes(value);
}
/// <summary>
/// 以字节数组的形式获取指定编码的字符串。
/// </summary>
/// <param name="value">要转换的字符串。</param>
/// <param name="encoding">要使用的编码。</param>
/// <param name="buffer">用于存放结果的字节数组。</param>
/// <returns>buffer 内实际填充了多少字节。</returns>
public static int GetBytes(string value, Encoding encoding, byte[] buffer)
{
return GetBytes(value, encoding, buffer, 0);
}
/// <summary>View on GitHub (pinned to d0c010b051)