{"record":{"id":"b31d6cf2dc6f9182","repo":"dotnet/wpf","slug":"sr-emptysctoreplace","errorCode":null,"errorMessage":"SR.EmptyScToReplace","messagePattern":"SR\\.EmptyScToReplace","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection.cs","lineNumber":448,"sourceCode":"\n            //add the strokes\n            //bypass this.AddRange, which calls changed events\n            //and call our protected List<Stroke> directly\n            ( (List<Stroke>)this.Items ).AddRange(strokes);\n\n            RaiseStrokesChanged(strokes, removedStrokes: null, index);\n        }\n\n        /// <summary>\n        /// Replace\n        /// </summary>\n        /// <param name=\"strokeToReplace\"></param>\n        /// <param name=\"strokesToReplaceWith\"></param>\n        public void Replace(Stroke strokeToReplace, StrokeCollection strokesToReplaceWith)\n        {\n            if ( strokeToReplace == null )\n            {\n                throw new ArgumentNullException(SR.EmptyScToReplace);\n            }\n\n            StrokeCollection strokesToReplace = new StrokeCollection();\n            strokesToReplace.Add(strokeToReplace);\n            this.Replace(strokesToReplace, strokesToReplaceWith);\n        }\n\n        /// <summary>\n        /// Replace\n        /// </summary>\n        /// <param name=\"strokesToReplace\"></param>\n        /// <param name=\"strokesToReplaceWith\"></param>\n        public void Replace(StrokeCollection strokesToReplace, StrokeCollection strokesToReplaceWith)\n        {\n            if ( strokesToReplace == null )\n            {\n                throw new ArgumentNullException(SR.EmptyScToReplace);\n            }","sourceCodeStart":430,"sourceCodeEnd":466,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Ink/StrokeCollection.cs#L430-L466","documentation":"The single-stroke overload StrokeCollection.Replace(Stroke, StrokeCollection) throws ArgumentNullException with SR.EmptyScToReplace when strokeToReplace is null. The stroke to replace must be a valid (and contained) stroke; null is rejected up front before delegating to the collection overload.","triggerScenarios":"Calling Replace(null, replacementStrokes); typically a lookup (FirstOrDefault, hit-test result, event-args field) returned null and its result was passed straight into Replace.","commonSituations":"Replacing a stroke found via hit-testing where the hit test missed; storing a stroke reference in a field that was reset; InkCanvas selection changed handlers passing null selection.","solutions":["Null-check strokeToReplace before calling Replace and skip or log when null.","Ensure the stroke reference is obtained from a successful lookup (e.g. hit-test or collection member) before replacing.","Catch ArgumentNullException around Replace for defensive UI code."],"exampleFix":"// before\nstrokes.Replace(foundStroke, new StrokeCollection { drawn });\n// after\nif (foundStroke != null && strokes.Contains(foundStroke))\n    strokes.Replace(foundStroke, new StrokeCollection { drawn });","handlingStrategy":"validation","validationCode":"if (strokeToReplace != null && strokes.Contains(strokeToReplace))\n    strokes.Replace(strokeToReplace, replacement);","typeGuard":"static bool CanReplace(StrokeCollection c, Stroke s) => s != null && c.Contains(s);","tryCatchPattern":"try { strokes.Replace(strokeToReplace, replacement); }\ncatch (ArgumentNullException) { /* strokeToReplace was null: skip */ }","preventionTips":["Null-check any stroke obtained from lookups/hit tests before replacing.","Keep stroke references in non-nullable fields initialized from the collection.","Handle InkCanvas selection-empty cases before calling Replace."],"tags":["wpf","ink","null-argument","replace"],"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"}