{"record":{"id":"ac028503282db0d7","repo":"tui-cs/Terminal.Gui","slug":"defaultkeybindings-dictionary-is-null-initialize","errorCode":null,"errorMessage":"DefaultKeyBindings dictionary is null. Initialize it before setting individual bindings.","messagePattern":"DefaultKeyBindings dictionary is null\\. Initialize it before setting individual bindings\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/App/Application.cs","lineNumber":344,"sourceCode":"\n    /// <summary>\n    ///     Raised when the <see cref=\"DefaultKeyBindings\"/> are changed — either by replacing the entire dictionary\n    ///     (property setter) or by calling <see cref=\"SetDefaultKeyBinding\"/> / <see cref=\"RemoveDefaultKeyBinding\"/>.\n    /// </summary>\n    public static event EventHandler? DefaultKeyBindingsChanged;\n\n    /// <summary>\n    ///     Sets (or replaces) the key binding for a single <paramref name=\"command\"/> in <see cref=\"DefaultKeyBindings\"/>\n    ///     and raises <see cref=\"DefaultKeyBindingsChanged\"/> so that subscribers (e.g. <c>ApplicationKeyboard</c>)\n    ///     pick up the change immediately.\n    /// </summary>\n    /// <param name=\"command\">The command whose binding should be set.</param>\n    /// <param name=\"binding\">The platform key binding to associate with <paramref name=\"command\"/>.</param>\n    public static void SetDefaultKeyBinding (Command command, PlatformKeyBinding binding)\n    {\n        if (DefaultKeyBindings is null)\n        {\n            throw new InvalidOperationException (\"DefaultKeyBindings dictionary is null. Initialize it before setting individual bindings.\");\n        }\n\n        DefaultKeyBindings [command] = binding;\n        Trace.Configuration (\"DefaultKeyBindings\", \"SetDefaultKeyBinding\", $\"{command}=[{binding}]\");\n        DefaultKeyBindingsChanged?.Invoke (null, EventArgs.Empty);\n    }\n\n    /// <summary>\n    ///     Removes the key binding for <paramref name=\"command\"/> from <see cref=\"DefaultKeyBindings\"/>\n    ///     and raises <see cref=\"DefaultKeyBindingsChanged\"/> so that subscribers pick up the change immediately.\n    /// </summary>\n    /// <param name=\"command\">The command whose binding should be removed.</param>\n    /// <returns><see langword=\"true\"/> if the binding was found and removed; otherwise <see langword=\"false\"/>.</returns>\n    public static bool RemoveDefaultKeyBinding (Command command)\n    {\n        if (DefaultKeyBindings is null || !DefaultKeyBindings.Remove (command))\n        {\n            return false;","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/App/Application.cs#L326-L362","documentation":"Thrown by Application.SetDefaultKeyBinding when the static DefaultKeyBindings dictionary is null. The dictionary is field-initialized with sensible defaults (Quit, Suspend, NextTabStop, etc.), so it is only null if application code or a ConfigurationManager JSON theme explicitly assigned null to Application.DefaultKeyBindings. The guard exists because SetDefaultKeyBinding indexes directly into the dictionary and a null dereference would otherwise produce a less helpful NullReferenceException.","triggerScenarios":"Calling Application.SetDefaultKeyBinding(command, binding) after code somewhere set Application.DefaultKeyBindings = null. Common culprits: a config/theme JSON file that serialized the dictionary as null and applied it via ConfigurationManager, or a reset/cleanup routine that nullified the property, or test teardown that set it to null without restoring it.","commonSituations":"Loading a custom Terminal.Gui config JSON whose DefaultKeyBindings section is malformed or explicitly null; running unit tests that mutate DefaultKeyBindings and fail to restore the initialized dictionary in teardown; programmatic theme switching code that sets the whole dictionary and hits a null branch.","solutions":["Before calling SetDefaultKeyBinding, ensure Application.DefaultKeyBindings is non-null: if (Application.DefaultKeyBindings is null) Application.DefaultKeyBindings = new ();","Audit any code or config that assigns Application.DefaultKeyBindings (search for 'DefaultKeyBindings =' and ConfigurationManager.Apply calls) and ensure it never assigns null.","If a config JSON is responsible, validate the DefaultKeyBindings section is either omitted (to keep defaults) or a valid object, never null.","In test teardown, restore the field initializer instead of nullifying: reassign the default dictionary literal or call the reset routine that re-establishes defaults."],"exampleFix":"// before\nApplication.DefaultKeyBindings = null;\nApplication.SetDefaultKeyBinding (Command.Quit, Bind.All (Key.Q.WithCtrl));\n\n// after\nApplication.DefaultKeyBindings ??= new ();\nApplication.SetDefaultKeyBinding (Command.Quit, Bind.All (Key.Q.WithCtrl));","handlingStrategy":"validation","validationCode":"if (Application.DefaultKeyBindings is null)\n{\n    Application.DefaultKeyBindings = new Dictionary<Command, PlatformKeyBinding> ();\n}\nApplication.SetDefaultKeyBinding (Command.Quit, Bind.All (Key.Q.WithCtrl));","typeGuard":"static bool HasDefaultKeyBindings () => Application.DefaultKeyBindings is not null;","tryCatchPattern":null,"preventionTips":["Never assign null to Application.DefaultKeyBindings; assign a new empty dictionary if you need to clear it.","Search the codebase for 'DefaultKeyBindings =' assignments and audit each one.","Validate theme/config JSON before applying — a null DefaultKeyBindings section will nullify the property."],"tags":["config","keybindings","static-state","initialization"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}