{"record":{"id":"ea279fcfe45626c9","repo":"EllanJiang/GameFramework","slug":"value-is-invalid","errorCode":null,"errorMessage":"Value is invalid.","messagePattern":"Value is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Utility/Utility.Converter.cs","lineNumber":735,"sourceCode":"            /// <param name=\"buffer\">用于存放结果的字节数组。</param>\n            /// <param name=\"startIndex\">buffer 内的起始位置。</param>\n            /// <returns>buffer 内实际填充了多少字节。</returns>\n            public static int GetBytes(string value, byte[] buffer, int startIndex)\n            {\n                return GetBytes(value, Encoding.UTF8, buffer, startIndex);\n            }\n\n            /// <summary>\n            /// 以字节数组的形式获取指定编码的字符串。\n            /// </summary>\n            /// <param name=\"value\">要转换的字符串。</param>\n            /// <param name=\"encoding\">要使用的编码。</param>\n            /// <returns>用于存放结果的字节数组。</returns>\n            public static byte[] GetBytes(string value, Encoding encoding)\n            {\n                if (value == null)\n                {\n                    throw new GameFrameworkException(\"Value is invalid.\");\n                }\n\n                if (encoding == null)\n                {\n                    throw new GameFrameworkException(\"Encoding is invalid.\");\n                }\n\n                return encoding.GetBytes(value);\n            }\n\n            /// <summary>\n            /// 以字节数组的形式获取指定编码的字符串。\n            /// </summary>\n            /// <param name=\"value\">要转换的字符串。</param>\n            /// <param name=\"encoding\">要使用的编码。</param>\n            /// <param name=\"buffer\">用于存放结果的字节数组。</param>\n            /// <returns>buffer 内实际填充了多少字节。</returns>\n            public static int GetBytes(string value, Encoding encoding, byte[] buffer)","sourceCodeStart":717,"sourceCodeEnd":753,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Utility/Utility.Converter.cs#L717-L753","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar bytes = Utility.Converter.GetTextBytes(message, Encoding.UTF8);\n// after\nvar bytes = message != null\n    ? Utility.Converter.GetBytes(message, Encoding.UTF8)\n    : Array.Empty<byte>();","handlingStrategy":"validation","validationCode":"if (value == null) return Array.Empty<byte>(); // or throw with domain context","typeGuard":"bool IsEncodable(string value) => value != null;","tryCatchPattern":"try { bytes = Utility.Converter.GetBytes(text, encoding); }\ncatch (GameFrameworkException ex) { bytes = Array.Empty<byte>(); /* log null text */ }","preventionTips":["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"],"tags":["null-argument","encoding","serialization"],"backgroundTag":"null-argument","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}