{"record":{"id":"864711382d73945e","repo":"pardeike/Harmony","slug":"value-cannot-be-null-parameter-config-methodbase","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'config.methodbase')","messagePattern":"Value cannot be null\\. \\(Parameter 'config\\.methodbase'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCopier.cs","lineNumber":29,"sourceCode":"{\r\n\tinternal class MethodCopier\r\n\t{\r\n\t\treadonly MethodBodyReader reader;\r\n\t\treadonly List<MethodInfo> transpilers = [];\r\n\r\n\t\tinternal MethodCopier(MethodBase fromMethod, ILGenerator toILGenerator, LocalBuilder[] existingVariables = null)\r\n\t\t{\r\n\t\t\tif (fromMethod is null)\r\n\t\t\t\tthrow new ArgumentNullException(nameof(fromMethod));\r\n\t\t\treader = new MethodBodyReader(fromMethod, toILGenerator);\r\n\t\t\treader.DeclareVariables(existingVariables);\r\n\t\t\treader.GenerateInstructions();\r\n\t\t}\r\n\r\n\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","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCopier.cs#L11-L47","documentation":"The MethodCreatorConfig constructor of MethodCopier reads the source method from config.MethodBase. If the config object was built without a MethodBase, Harmony throws ArgumentNullException explicitly labelled 'config.methodbase'.","triggerScenarios":"Calling MethodCopier(MethodCreatorConfig) where config.MethodBase is null — e.g. a MethodCreatorConfig created with only an ILGenerator/locals and never assigned the source method it should copy from.","commonSituations":"Programmatically generating replacement patch methods and forgetting to set the original MethodBase on the config; copying config-construction code that omitted the method assignment; refactors renaming the property so the setter call no longer binds.","solutions":["Set config.MethodBase to the source method before passing the config to MethodCopier","Check your MethodCreatorConfig construction code for a missing method assignment","Assert/log the config state right before constructing the copier to catch the null early","If the method may be absent, resolve it defensively and fail with a clearer message of your own"],"exampleFix":"// before\nvar config = new MethodCreatorConfig { il = generator };\nvar copier = new MethodCopier(config);\n// after\nvar config = new MethodCreatorConfig { il = generator, MethodBase = originalMethod };\nvar copier = new MethodCopier(config);","handlingStrategy":"validation","validationCode":"if (config.MethodBase is null) throw new InvalidOperationException(\"MethodCreatorConfig.MethodBase must be set before constructing MethodCopier\");","typeGuard":null,"tryCatchPattern":"try { var copier = new MethodCopier(config); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"config.methodbase\") { /* set MethodBase and rebuild config */ }","preventionTips":["Always assign MethodBase when building MethodCreatorConfig","Use object initializer syntax so all required fields are visible at the construction site","Assert config completeness right after building it"],"tags":["null-argument","methodcreator","il-copy","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"}