{"record":{"id":"f89159a8d223becd","repo":"dotnet/wpf","slug":"sr-stylus-pluginisnull","errorCode":null,"errorMessage":"SR.Stylus_PlugInIsNull","messagePattern":"SR\\.Stylus_PlugInIsNull","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPlugInCollection.cs","lineNumber":38,"sourceCode":"    public sealed class StylusPlugInCollection : Collection<StylusPlugIn>\n    {\n        #region Protected APIs\n\n        /// <summary>\n        /// Insert a StylusPlugIn in the collection at a specific index. \n        /// This method should be called from the application context only\n        /// </summary>\n        /// <param name=\"index\">index at which to insert the StylusPlugIn object</param>\n        /// <param name=\"plugIn\">StylusPlugIn object to insert, downcast to an object</param>\n        protected override void InsertItem(int index, StylusPlugIn plugIn)\n        {\n            // Verify it's called from the app dispatcher\n            _element.VerifyAccess();\n\n            // Validate the input parameter\n            if (null == plugIn)\n            {\n                throw new ArgumentNullException(nameof(plugIn), SR.Stylus_PlugInIsNull);\n            }\n\n            if (IndexOf(plugIn) != -1)\n            {\n                throw new ArgumentException(SR.Stylus_PlugInIsDuplicated, nameof(plugIn));\n            }\n\n            // Disable processing of the queue during blocking operations to prevent unrelated reentrancy\n            // which a call to Lock() can cause.\n            ExecuteWithPotentialDispatcherDisable(() =>\n            {\n                if (_stylusPlugInCollectionImpl.IsActiveForInput)\n                {\n                    // If we are currently active for input then we have a _penContexts that we must lock!\n                    ExecuteWithPotentialLock(() =>\n                     {\n                         System.Diagnostics.Debug.Assert(this.Count > 0); // If active must have more than one plugin already\n                         base.InsertItem(index, plugIn);","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/StylusPlugInCollection.cs#L20-L56","documentation":"StylusPlugInCollection.InsertItem (invoked by collection Add/Insert) verifies dispatcher access and rejects a null plug-in with ArgumentNullException carrying SR.Stylus_PlugInIsNull. A StylusPlugInCollection can only contain real StylusPlugIn instances.","triggerScenarios":"Calling stylusPlugIns.Add(null) or stylusPlugIns.Insert(index, null) on a UIElement's StylusPlugIns collection from the UI thread.","commonSituations":"A factory/locator method that resolves a StylusPlugIn lazily returns null (DI misconfiguration, missing assembly/type), and the null is passed straight to Add.","solutions":["Check the plug-in instance for null before adding it to the collection","Fix whatever produces the null plug-in (DI registration, type resolution, resource loading)","Ensure the code runs on the element's dispatcher thread to avoid the VerifyAccess failure that can precede this check"],"exampleFix":"// before\ninkCanvas.StylusPlugIns.Add(GetMyPlugIn()); // GetMyPlugIn may return null\n// after\nvar plugin = GetMyPlugIn();\nif (plugin != null)\n{\n    inkCanvas.StylusPlugIns.Add(plugin);\n}","handlingStrategy":"validation","validationCode":"if (plugin is null) throw new InvalidOperationException(\"StylusPlugIn not initialized\");\ninkCanvas.StylusPlugIns.Add(plugin);","typeGuard":"static bool IsValidPlugIn(StylusPlugIn p) => p is not null;","tryCatchPattern":"try { plugIns.Add(plugin); } catch (ArgumentNullException ex) when (ex.ParamName == \"plugIn\") { /* fix plug-in construction */ }","preventionTips":["Null-check plug-ins from factories/DI before adding","Ensure the plug-in type resolved correctly (assembly present)"],"tags":["wpf","stylus","null-argument"],"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"}