{"record":{"id":"6c44a32c8e99e199","repo":"pardeike/Harmony","slug":"null-method-for-instance-id-reversepatcher","errorCode":null,"errorMessage":"Null method for {instance.Id}","messagePattern":"Null method for (.+?)","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"Harmony/Public/ReversePatcher.cs","lineNumber":35,"sourceCode":"\t\t/// <param name=\"instance\">The Harmony instance</param>\r\n\t\t/// <param name=\"original\">The original method/constructor</param>\r\n\t\t/// <param name=\"standin\">Your stand-in stub method as <see cref=\"HarmonyMethod\"/></param>\r\n\t\t///\r\n\t\tpublic ReversePatcher(Harmony instance, MethodBase original, HarmonyMethod standin)\r\n\t\t{\r\n\t\t\tthis.instance = instance;\r\n\t\t\tthis.original = original;\r\n\t\t\tthis.standin = standin;\r\n\t\t}\r\n\r\n\t\t/// <summary>Applies the patch</summary>\r\n\t\t/// <param name=\"type\">The type of patch, see <see cref=\"HarmonyReversePatchType\"/></param>\r\n\t\t/// <returns>The generated replacement method</returns>\r\n\t\t///\r\n\t\tpublic MethodInfo Patch(HarmonyReversePatchType type = HarmonyReversePatchType.Original)\r\n\t\t{\r\n\t\t\tif (original is null)\r\n\t\t\t\tthrow new NullReferenceException($\"Null method for {instance.Id}\");\r\n\r\n\t\t\tstandin.reversePatchType = type;\r\n\t\t\tvar transpiler = GetTranspiler(standin.method);\r\n\t\t\treturn PatchFunctions.ReversePatch(standin, original, transpiler);\r\n\t\t}\r\n\r\n\t\tinternal static MethodInfo GetTranspiler(MethodInfo method)\r\n\t\t{\r\n\t\t\tvar methodName = method.Name;\r\n\t\t\tvar type = method.DeclaringType;\r\n\t\t\tvar methods = AccessTools.GetDeclaredMethods(type);\r\n\t\t\tvar ici = typeof(IEnumerable<CodeInstruction>);\r\n\t\t\treturn methods.FirstOrDefault(m =>\r\n\t\t\t{\r\n\t\t\t\tif (m.ReturnType != ici)\r\n\t\t\t\t\treturn false;\r\n\t\t\t\treturn m.Name.StartsWith($\"<{methodName}>\");\r\n\t\t\t});\r","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Public/ReversePatcher.cs#L17-L53","documentation":"ReversePatcher.Patch() creates a stand-alone copy of an original (unpatched) method body via a reverse transpiler. It requires a valid original MethodInfo; when the standin/original is null it throws a NullReferenceException with the Harmony instance id.","triggerScenarios":"Calling Patch() on a ReversePatcher created with a null original — e.g. harmony.CreateReversePatcher(AccessTools.Method(...) that returned null, or GetMethod called on a null standin method).","commonSituations":"Unpatching-by-reverse-patching after a target lookup failed due to a typo or an updated target assembly where the method was renamed or moved.","solutions":["Fix the method lookup so a valid original MethodInfo is passed to CreateReversePatcher","Null-check the MethodInfo before creating the ReversePatcher","Confirm the target method still exists and is a normal (non-abstract) method in the current target version"],"exampleFix":"// before\nvar patcher = harmony.CreateReversePatcher(AccessTools.Method(typeof(Player), \"Original\"));\nvar unpatched = patcher.Patch();\n// after\nvar original = AccessTools.Method(typeof(Player), \"Original\");\nif (original is null) throw new Exception(\"Player.Original not found\");\nvar unpatched = harmony.CreateReversePatcher(original).Patch();","handlingStrategy":"validation","validationCode":"var original = AccessTools.Method(targetType, methodName);\nif (original is null)\n    throw new InvalidOperationException($\"Reverse patch target {targetType}.{methodName} not found\");","typeGuard":null,"tryCatchPattern":"try { var unpatched = reversePatcher.Patch(); }\ncatch (NullReferenceException ex) when (ex.Message.StartsWith(\"Null method for\")) { logger.Error(\"Reverse patch target unresolved\"); }","preventionTips":["Null-check the MethodInfo before CreateReversePatcher","Only reverse-patch concrete methods with IL bodies","Re-resolve targets on load since game/library updates can rename methods"],"tags":["csharp","reflection","null-reference","reverse-patch","harmony"],"backgroundTag":"null-argument","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"}