EllanJiang/GameFramework · error · GameFrameworkException
Value is invalid.
Error message
Value is invalid.
What it means
Utility.Converter.GetBytes(string value, Encoding encoding) throws this when the string value is null. Encoding a null string has no defined byte representation in this helper, so it fails fast with a GameFrameworkException instead of returning null or an empty array. The encoding null check follows immediately.
Solutions
- Replace null with string.Empty before calling if an empty payload is acceptable.
- Guard the value with a null check and handle the null case explicitly (skip field, use default).
- Fix the producer of the string so it returns a real value or empty string.
- Catch GameFrameworkException at the serialization boundary for untrusted input.
Example fix
// before
var bytes = Utility.Converter.GetTextBytes(message, Encoding.UTF8);
// after
var bytes = message != null
? Utility.Converter.GetBytes(message, Encoding.UTF8)
: Array.Empty<byte>(); Defensive patterns
Strategy: validation
Validate before calling
if (value == null) return Array.Empty<byte>(); // or throw with domain context
Type guard
bool IsEncodable(string value) => value != null;
Try / catch
try { bytes = Utility.Converter.GetBytes(text, encoding); }
catch (GameFrameworkException ex) { bytes = Array.Empty<byte>(); /* log null text */ } Prevention
- Normalize optional strings to string.Empty early
- Give config-loaded strings defaults at load time
- Never pass through nullable string fields to low-level converters unchecked
When it happens
Trigger: Calling Utility.Converter.GetBytes(null, Encoding.UTF8) — e.g. serializing a message field that was never assigned, or a lookup that returned null for the text to send.
Common situations: Chat/network messages where an optional string field is null; config values read as null before defaults are applied; Unity UI text fields yielding null instead of empty string.
Related errors
- Buffer is invalid.
- Deserialize callback is invalid.
- Encoding is invalid.
- Serialize callback is invalid.
- Try get value callback is invalid.
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/ea279fcfe45626c9.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Utility/Utility.Converter.cs:735
/// <param name="buffer">用于存放结果的字节数组。</param>
/// <param name="startIndex">buffer 内的起始位置。</param>
/// <returns>buffer 内实际填充了多少字节。</returns>
public static int GetBytes(string value, byte[] buffer, int startIndex)
{
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)View on GitHub (pinned to d0c010b051)