{"record":{"id":"73d1c5d6c2234c9a","repo":"pardeike/Harmony","slug":"value-cannot-be-null-parameter-generator","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'generator')","messagePattern":"Value cannot be null\\. \\(Parameter 'generator'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCopier.cs","lineNumber":44,"sourceCode":"\t\tinternal MethodCopier(MethodCreatorConfig config)\r\n\t\t{\r\n\t\t\tif (config.MethodBase is null)\r\n\t\t\t\tthrow new ArgumentNullException(\"config.methodbase\");\r\n\t\t\treader = new MethodBodyReader(config.MethodBase, config.il);\r\n\t\t\treader.DeclareVariables(config.originalVariables);\r\n\t\t\treader.GenerateInstructions();\r\n\t\t\treader.SetDebugging(config.debug);\r\n\t\t}\r\n\r\n\t\tinternal void AddTranspiler(MethodInfo transpiler) => transpilers.Add(transpiler);\r\n\r\n\t\tinternal List<CodeInstruction> Finalize(bool stripLastReturn, out bool hasReturnCode, out bool methodEndsInDeadCode, List<Label> endLabels)\r\n\t\t\t=> reader.FinalizeILCodes(transpilers, stripLastReturn, out hasReturnCode, out methodEndsInDeadCode, endLabels);\r\n\r\n\t\tinternal static List<CodeInstruction> GetInstructions(ILGenerator generator, MethodBase method, int maxTranspilers)\r\n\t\t{\r\n\t\t\tif (generator is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(generator));\r\n\t\t\tif (method is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(method));\r\n\r\n\t\t\tvar originalVariables = MethodPatcherTools.DeclareOriginalLocalVariables(generator, method);\r\n\t\t\tvar copier = new MethodCopier(method, generator, originalVariables);\r\n\r\n\t\t\tvar info = Harmony.GetPatchInfo(method);\r\n\t\t\tif (info is not null)\r\n\t\t\t{\r\n\t\t\t\tvar sortedTranspilers = PatchFunctions.GetSortedPatchMethods(method, [.. info.Transpilers], false);\r\n\t\t\t\tfor (var i = 0; i < maxTranspilers && i < sortedTranspilers.Count; i++)\r\n\t\t\t\t\tcopier.AddTranspiler(sortedTranspilers[i]);\r\n\t\t\t}\r\n\r\n\t\t\treturn copier.Finalize(false, out _, out _, null);\r\n\t\t}\r\n\t}\r\n\r","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCopier.cs#L26-L62","documentation":"MethodCopier.GetInstructions copies a method's instructions into a caller-supplied ILGenerator so transpilers can run on them. The generator is essential (original locals are declared on it), so a null generator is rejected with ArgumentNullException('generator').","triggerScenarios":"Calling MethodCopier.GetInstructions(null, method, maxTranspilers) or a patch pipeline that resolved its ILGenerator lazily and got null (e.g. MethodPatcher building a replacement body without a valid generator).","commonSituations":"Custom patch tooling built on Harmony internals passing a generator created conditionally; refactors where the generator creation step was skipped; using GetInstructions outside Harmony's normal patch flow without creating a DynamicMethod/ILGenerator first.","solutions":["Create a valid ILGenerator (e.g. from a DynamicMethod or MethodBuilder) and pass it to GetInstructions","Trace where your generator value comes from and ensure it is initialized before the call","If using Harmony's high-level Patch API instead, the generator is created for you — prefer harmony.Patch over raw internals","Add a guard that fails early with your own message when the generator could not be created"],"exampleFix":"// before\nvar instructions = MethodCopier.GetInstructions(null, originalMethod, 3);\n// after\nvar dm = new DynamicMethod(\"Copy\", originalMethod.ReturnType, originalMethod.GetParameters().Select(p => p.ParameterType).ToArray());\nvar instructions = MethodCopier.GetInstructions(dm.GetILGenerator(), originalMethod, 3);","handlingStrategy":"validation","validationCode":"if (generator is null) throw new InvalidOperationException(\"ILGenerator must be created before calling MethodCopier.GetInstructions\");","typeGuard":null,"tryCatchPattern":"try { var instrs = MethodCopier.GetInstructions(generator, method, maxTranspilers); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"generator\") { /* create DynamicMethod/ILGenerator and retry */ }","preventionTips":["Create the ILGenerator from a DynamicMethod before touching Harmony internals","Prefer the high-level harmony.Patch API which manages generators itself","Null-check lazily created generators before use"],"tags":["null-argument","ilgenerator","transpiler","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"}