{"record":{"id":"1d45e2bb71494967","repo":"pardeike/Harmony","slug":"method-method-fulldescription-returned-an-unexpected-result","errorCode":null,"errorMessage":"Method {method.FullDescription()} returned an unexpected result: {error}","messagePattern":"Method (.+?) returned an unexpected result: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Harmony/Public/PatchClassProcessor.cs","lineNumber":276,"sourceCode":"\t\t\t\tlist.AddRange(props.Select(prop => prop.GetSetMethod(true)).Where(method => method is not null).Cast<MethodBase>());\r\n\t\t\t\treturn list;\r\n\t\t\t}\r\n\r\n\t\t\tvar result = new List<MethodBase>();\r\n\r\n\t\t\tvar targetMethods = RunMethod<HarmonyTargetMethods, IEnumerable<MethodBase>>(null, null);\r\n\t\t\tif (targetMethods is object)\r\n\t\t\t{\r\n\t\t\t\tstring error = null;\r\n\t\t\t\tresult = [.. targetMethods];\r\n\t\t\t\tif (result is null)\r\n\t\t\t\t\terror = \"null\";\r\n\t\t\t\telse if (result.Any(m => m is null))\r\n\t\t\t\t\terror = \"some element was null\";\r\n\t\t\t\tif (error != null)\r\n\t\t\t\t{\r\n\t\t\t\t\tif (auxilaryMethods.TryGetValue(typeof(HarmonyTargetMethods), out var method))\r\n\t\t\t\t\t\tthrow new Exception($\"Method {method.FullDescription()} returned an unexpected result: {error}\");\r\n\t\t\t\t\telse\r\n\t\t\t\t\t\tthrow new Exception($\"Some method returned an unexpected result: {error}\");\r\n\t\t\t\t}\r\n\t\t\t\treturn result;\r\n\t\t\t}\r\n\r\n\t\t\tvar targetMethod = RunMethod<HarmonyTargetMethod, MethodBase>(null, null, method => method is null ? \"null\" : null);\r\n\t\t\tif (targetMethod is not null)\r\n\t\t\t\tresult.Add(targetMethod);\r\n\r\n\t\t\treturn result;\r\n\t\t}\r\n\r\n\t\tvoid ReportException(Exception exception, MethodBase original)\r\n\t\t{\r\n\t\t\tif (exception is null)\r\n\t\t\t\treturn;\r\n\t\t\tif ((containerAttributes.debug ?? false) || Harmony.DEBUG)\r","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Public/PatchClassProcessor.cs#L258-L294","documentation":"A HarmonyTargetMethods auxiliary method must return a non-empty IEnumerable<MethodBase> with no null elements. When it returns null or an empty/null-containing sequence, Harmony wraps the failure in an Exception identifying the method via FullDescription(), since there is otherwise nothing to patch.","triggerScenarios":"A TargetMethods() method returning null; returning an empty IEnumerable; yielding null elements (e.g. AccessTools.Method returning null for a missing overload collected into a list).","commonSituations":"Collecting targets with 'yield return AccessTools.Method(...)' where some lookups return null after a version change; conditionally returning no methods when a feature is absent.","solutions":["Filter nulls in TargetMethods(): use 'var m = AccessTools.Method(...); if (m != null) yield return m;'","Never return null — return an empty sequence only when intentionally patching nothing, and guard the caller","Log/skip missing targets explicitly so you control the failure instead of Harmony"],"exampleFix":"// before\nstatic IEnumerable<MethodBase> TargetMethods() =>\n    new[] { AccessTools.Method(typeof(Foo), \"Bar\"), AccessTools.Method(typeof(Foo), \"Baz\") };\n// after\nstatic IEnumerable<MethodBase> TargetMethods()\n{\n    foreach (var name in new[] { \"Bar\", \"Baz\" })\n    {\n        var m = AccessTools.Method(typeof(Foo), name);\n        if (m != null) yield return m;\n        else FileLog.Log($\"Foo.{name} not found, skipping\");\n    }\n}","handlingStrategy":"validation","validationCode":"var methods = TargetMethods();\nif (methods == null || methods.Any(m => m == null)) throw new InvalidOperationException(\"TargetMethods must return non-null MethodBase elements\");","typeGuard":"static bool IsValidTargets(IEnumerable<MethodBase> ms) => ms != null && ms.All(m => m != null);","tryCatchPattern":"try { harmony.PatchAll(); } catch (Exception e) when (e.Message.Contains(\"returned an unexpected result\")) { LogNullTargets(e); }","preventionTips":["Never return null from TargetMethods(); filter null AccessTools results before yielding","Log skipped targets instead of yielding nulls"],"tags":["target-methods","null-result","empty-sequence","harmony"],"backgroundTag":"empty-result-set","analyzedSha":"e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c","analyzedAt":"2026-09-15T22:47:11.550Z","contentChangedAt":"2026-09-15T22:47:11.550Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}