{"record":{"id":"b1dff31bea8bc0a7","repo":"dotnet/wpf","slug":"the-exception-argument-cannot-be-null-b1dff3","errorCode":null,"errorMessage":"The exception argument cannot be null.","messagePattern":"The exception argument cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMappingExceptionEventArgs.cs","lineNumber":24,"sourceCode":"namespace System.Windows.Forms.Integration\n{\n    /// <summary>\n    /// Enables the user to see the property that threw an exception, and to preview or cancel the exception.\n    /// </summary>\n    public class PropertyMappingExceptionEventArgs : IntegrationExceptionEventArgs \n    {\n        private string _propertyName;\n        private object _propertyValue;\n\n        /// <summary>\n        /// Initializes a new instance of the PropertyMappingExceptionEventArgs class.\n        /// </summary>\n        public PropertyMappingExceptionEventArgs(Exception exception, string propertyName, object propertyValue) \n            : base(false, exception)\n        {\n            if (exception == null)\n            {\n                throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture, SR.WFI_NullArgument, \"exception\"));\n            }\n            if (string.IsNullOrEmpty(propertyName))\n            {\n                throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture, SR.WFI_ArgumentNullOrEmpty, \"propertyName\"));\n            }\n            _propertyName = propertyName;\n            _propertyValue = propertyValue;\n        }\n\n        /// <summary>\n        /// Identifies the property that was being mapped when the exception occurred.\n        /// </summary>\n        public string PropertyName\n        {\n            get\n            {\n                return _propertyName;\n            }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsFormsIntegration/System/Windows/Integration/PropertyMappingExceptionEventArgs.cs#L6-L42","documentation":"The PropertyMappingExceptionEventArgs constructor validates its arguments up front: the exception to report must be non-null because the event args exist solely to carry a mapping failure. Passing null throws ArgumentNullException naming 'exception'.","triggerScenarios":"Constructing PropertyMappingExceptionEventArgs(null, \"someProp\", value) inside a PropertyMappingError handler or test that builds the event args manually; catching and forwarding an exception variable that turned out to be null.","commonSituations":"Unit tests simulating mapping errors; wrapper code that stores the last exception in a field initialized to null and then builds event args from it.","solutions":["Pass the actual caught Exception instance into the constructor.","Guard before constructing: if (ex != null) new PropertyMappingExceptionEventArgs(ex, prop, value);","Remove code paths that synthesize event args without a real exception."],"exampleFix":"// before\nnew PropertyMappingExceptionEventArgs(lastError, \"Background\", value); // lastError may be null\n// after\nif (lastError != null)\n    new PropertyMappingExceptionEventArgs(lastError, \"Background\", value);","handlingStrategy":"validation","validationCode":"if (ex == null) throw new InvalidOperationException(\"Cannot report a mapping error without an exception\");\nvar args = new PropertyMappingExceptionEventArgs(ex, propertyName, value);","typeGuard":"bool CanBuildMappingEventArgs(Exception ex, string prop) => ex != null && !string.IsNullOrEmpty(prop);","tryCatchPattern":"try { var e = new PropertyMappingExceptionEventArgs(ex, prop, value); }\ncatch (ArgumentNullException) { log.Error(\"No exception captured to report\"); }","preventionTips":["Never build event args from a nullable exception field without a null check","Capture the exception inside the catch block and forward it immediately","In tests, always pass a concrete exception instance (e.g. new InvalidOperationException())"],"tags":["argument-null","event-args","constructor"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}