{"record":{"id":"763a9a25c49187d2","repo":"dotnet/wpf","slug":"argumentnullexception-handle","errorCode":null,"errorMessage":"ArgumentNullException: handle","messagePattern":"ArgumentNullException: handle","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs","lineNumber":893,"sourceCode":"    /// </summary>\n    internal class OleDropTarget : DispatcherObject, UnsafeNativeMethods.IOleDropTarget\n    {\n        //------------------------------------------------------\n        //\n        //  Constructor\n        //\n        //------------------------------------------------------\n        \n        #region Constructor\n\n        /// <summary>\n        /// OleDropTarget Constructor.\n        /// </summary>\n        public OleDropTarget(IntPtr handle)\n        {\n            if (handle == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(handle));\n            }\n\n            _windowHandle = handle;\n        }\n\n        #endregion Constructor\n\n        //------------------------------------------------------\n        //\n        //  IOleDropTarget Interface\n        //\n        //------------------------------------------------------\n\n        #region IOleDropTarget\n\n        /// <summary>\n        /// OleDragEnter - check the data object and notify DragEnter to the target element.\n        /// </summary>","sourceCodeStart":875,"sourceCodeEnd":911,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/DragDrop.cs#L875-L911","documentation":"The OleDropTarget constructor wraps a Win32 window handle (HWND) that will receive OLE drop notifications. Passing IntPtr.Zero means there is no target window, so the constructor throws ArgumentNullException named \"handle\" immediately rather than failing later in the drop protocol.","triggerScenarios":"Calling new OleDropTarget(IntPtr.Zero), typically when a window handle could not be obtained (e.g. HwndSource not yet created, control not initialized, or Handle property returning Zero).","commonSituations":"Registering a drop target during window initialization before the HWND exists; interop code that caches a handle before the window is shown; headless or disconnected window scenarios.","solutions":["Ensure the window/control is fully initialized (HWND created) before constructing OleDropTarget — e.g. wait for the Loaded event or HandleCreated.","Obtain the handle via source.Handle on an HwndSource created from the actual window, and verify it is non-zero.","If the handle comes from an external component, validate it is != IntPtr.Zero before constructing."],"exampleFix":"// before\nvar target = new OleDropTarget(control.Handle); // may be IntPtr.Zero\n// after\nif (control.IsHandleCreated && control.Handle != IntPtr.Zero)\n    var target = new OleDropTarget(control.Handle);\nelse\n    control.HandleCreated += (s, e) => new OleDropTarget(control.Handle);","handlingStrategy":"validation","validationCode":"if (handle == IntPtr.Zero) throw new InvalidOperationException(\"Cannot create OleDropTarget before the window handle exists\");","typeGuard":"static bool HasWindowHandle(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { var target = new OleDropTarget(handle); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"handle\") { /* defer until handle created */ }","preventionTips":["Register drop targets after the window is loaded (Loaded event / HandleCreated).","Use HwndSource.FromVisual(element).Handle rather than caching handles early.","Always check IntPtr.Zero on handles obtained from external components."],"tags":["wpf","drag-drop","ole","argument-null"],"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"}