I got the stack trace below reported from a customer. I don't know how to reproduce this. My WPF application has a fair number of ComboBoxes; I'm not sure how to determine which ComboBox failed given the stack trace below. Has anyone else seen this? Can you interpret what was going on from this stack trace? Any ideas?
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Controls.ComboBox.CoerceIsSelectionBoxHighlighted(Object o, Object value)
at System.Windows.DependencyObject.ProcessCoerceValue(DependencyProperty dp, PropertyMetadata metadata, EntryIndex& entryIndex, Int32& targetIndex, EffectiveValueEntry& newEntry, EffectiveValueEntry& oldEntry, Object& oldValue, Object baseValue, Object controlValue, CoerceValueCallback coerceValueCallback, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, Boolean skipBaseValueChecks)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.CoerceValue(DependencyProperty dp)
at System.Windows.Controls.ComboBox.OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FocusWithinProperty.FireNotifications(UIElement uie, ContentElement ce, UIElement3D uie3D, Boolean oldValue)
at System.Windows.ReverseInheritProperty.FirePropertyChangeInAncestry(DependencyObject element, Boolean oldValue, DeferredElementTreeState treeState, Action`2 originChangedAction)
at System.Windows.ReverseInheritProperty.OnOriginValueChanged(DependencyObject oldOrigin, DependencyObject newOrigin, IList`1 otherOrigins, DeferredElementTreeState& oldTreeState, Action`2 originChangedAction)
at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
at System.Windows.Input.KeyboardDevice.PostProcessInput(Object sender, ProcessInputEventArgs e)
at System.Windows.Input.InputManager.RaiseProcessInputEventHandlers(ProcessInputEventHandler postProcessInput, ProcessInputEventArgs processInputEventArgs)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
at System.Windows.Interop.HwndKeyboardInputProvider.PossiblyDeactivate(IntPtr hwndFocus)
at System.Windows.Interop.HwndKeyboardInputProvider.FilterMessage(IntPtr hwnd, WindowMessage message, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
Here's the code (.NET 4.5.2). Either
o
isn't aComboBox
orHighlightedElement
is null.Personally, my first step would be to distribute the PDBs so you could get line numbers in the stack trace.
What eventually resolved this for us was to override the event causing the issue:
I got the same error with similar code to what maiksaray shared. For me, the NullReferenceException at CoerceIsSelectionBoxHighlighted only happened on Windows 10, not on my Windows 7 dev machine. It only happened the first time the combo box was clicked to open.
In my case, I was programmatically opening and closing the combobox when the view loaded:
I was doing this as the workaround to another problem, described here: http://blog.elgaard.com/2009/09/03/wpf-making-combo-box-items-disabled-also-when-accessed-using-the-keyboard/
The error happened after DevicesComboBox_DropDownOpened completed. However, it only happened with the OnLoaded code present. If I commented out Loaded += OnLoaded, then I didn't get the error.
The solution for me was to simply avoid programmatically opening and closing the ComboBox.
We have a bespoke filtered combobox we made as a usercontrol that inherits from combobox. On Windows 10 machines we started to get this error for filtered comboboxes within forms on datagrid rows. We have a filtered combobox within the DataGrid.RowDetailsTemplate on a DataGrid.
To make the error go away we overrode this sub within our Filtered_Combobox class.
Note: We havent put any code in the override yet because it didnt seem to do anything (despite crash the application).
We had the similar problem on some(didn't have time to get to precise range) versions of runtime and windows.
One of our comboboxes had following style
It somehow, sometimes messed with selection highlighting and caused exception. Maybe this is helpful.