11

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)
Brannon
  • 5,324
  • 4
  • 35
  • 83
  • 2
    For what it's worth, I was having the same problem today and managed to solve it. I was using [SetWidthFromItems](http://stackoverflow.com/a/4414372/1492977) on my combobox. If I cleared the list and added new items without calling SetWidthFromItems, then I would get this error. If I called SetWidthFromItems after adding the elements, the error no longer occurred. – Anders Carstensen Apr 12 '15 at 16:16

5 Answers5

6

Here's the code (.NET 4.5.2). Either o isn't a ComboBox or HighlightedElement is null.

Personally, my first step would be to distribute the PDBs so you could get line numbers in the stack trace.

private static object CoerceIsSelectionBoxHighlighted(object o, object value)
{
    ComboBox comboBox = (ComboBox)o;
    return (!comboBox.IsDropDownOpen && comboBox.IsKeyboardFocusWithin) ||
           (comboBox.HighlightedInfo != null && comboBox.HighlightedElement.Content == comboBox._clonedElement);
}

private ComboBoxItem HighlightedElement
{
    get { return (_highlightedInfo == null) ? null : _highlightedInfo.Container as ComboBoxItem; }
}
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • @MyCodeSucks The method throwing the exception. Look at his stack trace. – Zer0 Mar 27 '15 at 17:41
  • Then he should be the one posting it. It doesn't belong in an answer. – PiousVenom Mar 27 '15 at 17:41
  • 1
    Perhaps he doesn't have it or hasn't seen it? Seeing the code shows where a possible null ref exception could come from. – Zer0 Mar 27 '15 at 17:44
  • Still does not belong in an answer. You can edit his question to put the relevant code if you think it will help his question. – PiousVenom Mar 27 '15 at 17:46
  • 9
    Thanks for posting the code. That almost looks like a typo in the WPF source code. They should have checked `HighlightedElement` for null rather than `HighlightedInfo`. – Brannon Mar 27 '15 at 18:02
  • 5
    Connect issue indicates fix will be in 4.6.2 release. https://connect.microsoft.com/VisualStudio/feedback/details/1660886/system-windows-controls-combobox-coerceisselectionboxhighlighted-bug – jbeanky May 26 '16 at 14:42
2

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.

Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs)
    Try


    Catch ex As Exception
    End Try
End Sub

Note: We havent put any code in the override yet because it didnt seem to do anything (despite crash the application).

2

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

<ComboBox.Style>
    <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
        <Setter Property="IsDropDownOpen" Value="False"/>
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="IsDropDownOpen" Value="True" />
                </Trigger>
            </Style.Triggers>
    </Style>

It somehow, sometimes messed with selection highlighting and caused exception. Maybe this is helpful.

maiksaray
  • 358
  • 2
  • 14
1

What eventually resolved this for us was to override the event causing the issue:

Protected Overrides Sub OnIsKeyboardFocusWithinChanged(e As DependencyPropertyChangedEventArgs)
    Try
        'GW 2015-09-20 Added this override to prevent windows 10 crashes on comboboxes within forms within datagrids

    Catch ex As Exception
    End Try
End Sub
0

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:

public MyView()
{
    InitializeComponent();
    Loaded += OnLoaded;
}

private void comboBox1_DropDownOpened(object sender, EventArgs e)
{
    comboBox1.ItemsSource = MyClass.GetComboBoxList();
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
   comboBox1.IsDropDownOpen = true;
   comboBox1.IsDropDownOpen = false;
}

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.