pardeike/Harmony · error · Exception
Method returned an unexpected result
Error message
Method {method.FullDescription()} returned an unexpected result: {error} What it means
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.
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
Example fix
// before
static IEnumerable<MethodBase> TargetMethods() =>
new[] { AccessTools.Method(typeof(Foo), "Bar"), AccessTools.Method(typeof(Foo), "Baz") };
// after
static IEnumerable<MethodBase> TargetMethods()
{
foreach (var name in new[] { "Bar", "Baz" })
{
var m = AccessTools.Method(typeof(Foo), name);
if (m != null) yield return m;
else FileLog.Log($"Foo.{name} not found, skipping");
}
} Defensive patterns
Strategy: validation
Validate before calling
var methods = TargetMethods();
if (methods == null || methods.Any(m => m == null)) throw new InvalidOperationException("TargetMethods must return non-null MethodBase elements"); Type guard
static bool IsValidTargets(IEnumerable<MethodBase> ms) => ms != null && ms.All(m => m != null);
Try / catch
try { harmony.PatchAll(); } catch (Exception e) when (e.Message.Contains("returned an unexpected result")) { LogNullTargets(e); } Prevention
- Never return null from TargetMethods(); filter null AccessTools results before yielding
- Log skipped targets instead of yielding nulls
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Some method returned an unexpected result
- The type must declare an empty constructor (the constructor…
- position( ) + count( ) > buffer.Length( )
- Unbalanced exception markers – cannot rewrite.
- Multiple loaded HarmonySharedState types prevent safe…
AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15).
Data as JSON: /api/errors/1d45e2bb71494967.
Report an issue: GitHub.
Appendix: source
Thrown at Harmony/Public/PatchClassProcessor.cs:276
list.AddRange(props.Select(prop => prop.GetSetMethod(true)).Where(method => method is not null).Cast<MethodBase>());
return list;
}
var result = new List<MethodBase>();
var targetMethods = RunMethod<HarmonyTargetMethods, IEnumerable<MethodBase>>(null, null);
if (targetMethods is object)
{
string error = null;
result = [.. targetMethods];
if (result is null)
error = "null";
else if (result.Any(m => m is null))
error = "some element was null";
if (error != null)
{
if (auxilaryMethods.TryGetValue(typeof(HarmonyTargetMethods), out var method))
throw new Exception($"Method {method.FullDescription()} returned an unexpected result: {error}");
else
throw new Exception($"Some method returned an unexpected result: {error}");
}
return result;
}
var targetMethod = RunMethod<HarmonyTargetMethod, MethodBase>(null, null, method => method is null ? "null" : null);
if (targetMethod is not null)
result.Add(targetMethod);
return result;
}
void ReportException(Exception exception, MethodBase original)
{
if (exception is null)
return;
if ((containerAttributes.debug ?? false) || Harmony.DEBUG)
View on GitHub (pinned to e7872dc170)