{"record":{"id":"00b9fe11448d745d","repo":"dotnet/wpf","slug":"argumentnullexception-argname","errorCode":null,"errorMessage":"ArgumentNullException(argName)","messagePattern":"ArgumentNullException\\(argName\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs","lineNumber":132,"sourceCode":"                        _triggers.Seal();\n                    }\n                }\n                return _triggers; \n            }\n        }\n\n        #endregion PublicProperties\n\n        #region NonPublicMethods\n\n        // Validate against two rules\n        //  1. targetType must not null\n        //  2. targetType must be a Control or a subclass of it\n        private void ValidateTargetType(Type targetType, string argName)\n        {\n            if (targetType == null)\n            {\n                throw new ArgumentNullException(argName);\n            }\n            if (!typeof(Control).IsAssignableFrom(targetType) &&\n                !typeof(Page).IsAssignableFrom(targetType) &&\n                !typeof(PageFunctionBase).IsAssignableFrom(targetType))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidControlTemplateTargetType, targetType.Name));\n            }\n        }\n        \n        #endregion NonPublicMethods\n\n        #region NonPublicProperties\n        \n        //\n        //  TargetType for ControlTemplate. This is override is \n        //  so FrameworkTemplate can see this property.\n        //\n        internal override Type TargetTypeInternal","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ControlTemplate.cs#L114-L150","documentation":"ControlTemplate.ValidateTargetType throws ArgumentNullException when a null TargetType is supplied to the ControlTemplate constructor or the TargetType setter. WPF requires every ControlTemplate to declare a non-null target type so it can validate the templated control.","triggerScenarios":"new ControlTemplate(null) or new ControlTemplate(typeof(X), null); assigning TargetType = null in code; XAML omitting TargetType where the parser passes null through code paths that validate.","commonSituations":"Building templates programmatically and forgetting to pass the type; dynamic template construction where the type variable is null; refactoring that removed the typeof(...) argument.","solutions":["Pass a concrete Control/Page/PageFunction-derived Type to the ControlTemplate constructor or TargetType setter.","Null-check the type variable before constructing the template.","In XAML always specify TargetType on the ControlTemplate."],"exampleFix":"// before\nvar tpl = new ControlTemplate(null);\n// after\nvar tpl = new ControlTemplate(typeof(Button));","handlingStrategy":"validation","validationCode":"if (targetType == null) throw new ArgumentException(\"TargetType is required\");","typeGuard":"bool HasTargetType(ControlTemplate t) => t?.TargetType != null;","tryCatchPattern":"try { var tpl = new ControlTemplate(type); } catch (ArgumentNullException) { /* supply a default control type */ }","preventionTips":["Null-check type variables before constructing templates","Always specify TargetType in XAML ControlTemplates"],"tags":["wpf","null-argument","controltemplate"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}