{"record":{"id":"38138e25a7e0e6f2","repo":"pardeike/Harmony","slug":"nameof-id-cannot-be-null-or-empty","errorCode":null,"errorMessage":"{nameof(id)} cannot be null or empty","messagePattern":"(.+?) cannot be null or empty","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Public/Harmony.cs","lineNumber":29,"sourceCode":"\t///\r\n\tpublic class Harmony\r\n\t{\r\n\t\t/// <summary>The unique identifier</summary>\r\n\t\t///\r\n\t\tpublic string Id { get; private set; }\r\n\r\n\t\t/// <summary>Set to true before instantiating Harmony to debug Harmony or use an environment variable to set HARMONY_DEBUG to '1' like this: cmd /C \"set HARMONY_DEBUG=1 &amp;&amp; game.exe\"</summary>\r\n\t\t/// <remarks>This is for full debugging. To debug only specific patches, use the <see cref=\"HarmonyDebug\"/> attribute</remarks>\r\n\t\t///\r\n\t\tpublic static bool DEBUG;\r\n\r\n\t\t/// <summary>Creates a new Harmony instance</summary>\r\n\t\t/// <param name=\"id\">A unique identifier (you choose your own)</param>\r\n\t\t/// <returns>A Harmony instance</returns>\r\n\t\t///\r\n\t\tpublic Harmony(string id)\r\n\t\t{\r\n\t\t\tif (string.IsNullOrEmpty(id)) throw new ArgumentException($\"{nameof(id)} cannot be null or empty\");\r\n\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tvar envDebug = Environment.GetEnvironmentVariable(\"HARMONY_DEBUG\");\r\n\t\t\t\tif (envDebug is not null && envDebug.Length > 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tenvDebug = envDebug.Trim();\r\n\t\t\t\t\tDEBUG = envDebug == \"1\" || bool.Parse(envDebug);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tcatch\r\n\t\t\t{\r\n\t\t\t}\r\n\r\n\t\t\tif (DEBUG)\r\n\t\t\t{\r\n\t\t\t\tvar assembly = typeof(Harmony).Assembly;\r\n\t\t\t\tvar version = assembly.GetName().Version;\r","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Public/Harmony.cs#L11-L47","documentation":"Guard clause validating the identifier passed to the Harmony constructor: a null or empty string was supplied as 'id'. Harmony requires a non-empty unique identifier per instance to scope patch registration and unregistration; without one, instances cannot be distinguished, so construction is aborted. This is a generic validation guard; the input at fault is the constructor argument 'id'.","triggerScenarios":"new Harmony(null), new Harmony(\"\"), or a configuration variable/env var supplying the id that resolves to null/empty at runtime.","commonSituations":"Mod frameworks reading the patch id from config or metadata files where the field is missing; tests constructing Harmony with a placeholder id that defaulted to empty.","solutions":["Pass a stable, non-empty unique id such as your mod's assembly name or a GUID","If the id comes from config, validate it before constructing Harmony and fail with a clear message","Use nameof(YourMod) or Guid.NewGuid().ToString() as a guaranteed non-empty source"],"exampleFix":"// before\nvar id = modConfig.PatchId;\nvar harmony = new Harmony(id);\n// after\nif (string.IsNullOrEmpty(modConfig.PatchId)) throw new InvalidOperationException(\"PatchId missing in mod config\");\nvar harmony = new Harmony(modConfig.PatchId);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(id)) throw new InvalidOperationException(\"Harmony id must be a non-empty unique string\");\nvar harmony = new Harmony(id);","typeGuard":"bool IsValidHarmonyId([NotNullWhen(true)] string? id) => !string.IsNullOrEmpty(id);","tryCatchPattern":"try { harmony = new Harmony(id); } catch (ArgumentException ex) { log.Error(\"Harmony id missing/empty\", ex); return; }","preventionTips":["Derive the id from your assembly name or a fixed GUID constant","Validate config-driven ids before constructing Harmony","Never share one id across unrelated patch sets"],"tags":["initialization","identifier","csharp"],"backgroundTag":"empty-required-field","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"}