We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

ComboBoxAdv.ValidateItem generates NullReferenceException

Dear customer support,

I have a propertygrid with a custom editor using a combobox. In some cases I get a null reference error when trying to select a value from the itemssource. 

Please see the stacktrace and code for the customeditor below.

Any advise would be appreciated.

Kind regards,

Niels van Strien


> Syncfusion.Shared.WPF.dll!Syncfusion.Windows.Tools.Controls.ComboBoxAdv.ValidateItem(System.Windows.Input.Key pressedkey) Unknown

  Syncfusion.Shared.WPF.dll!Syncfusion.Windows.Tools.Controls.ComboBoxAdv.Part_IsEdit_LostFocus(object sender, System.Windows.RoutedEventArgs e) Unknown

  PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown

  PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown

  PresentationFramework.dll!System.Windows.Controls.Primitives.TextBoxBase.OnLostFocus(System.Windows.RoutedEventArgs e) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  PresentationFramework.dll!System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  PresentationFramework.dll!System.Windows.Controls.TextBox.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs args) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.ClearValueCommon(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.ClearValue(System.Windows.DependencyPropertyKey key) Unknown

  PresentationCore.dll!System.Windows.Input.FocusManager.OnFocusedElementChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  PresentationFramework.dll!System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  Syncfusion.Tools.WPF.dll!Syncfusion.Windows.Tools.Controls.RibbonWindow.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs args) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.SetValueCommon(System.Windows.DependencyProperty dp, object value, System.Windows.PropertyMetadata metadata, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType, bool isInternal) Unknown

  WindowsBase.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) Unknown

  PresentationFramework.dll!System.Windows.Input.KeyboardNavigation.UpdateFocusedElement(System.Windows.DependencyObject focusTarget) Unknown

  PresentationFramework.dll!System.Windows.FrameworkElement.OnGotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e) Unknown

  PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown

  PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown

  PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown

  PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args) Unknown

  PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown

  PresentationCore.dll!System.Windows.Input.KeyboardDevice.ChangeFocus(System.Windows.DependencyObject focus, int timestamp) Unknown

  PresentationCore.dll!System.Windows.Input.KeyboardDevice.Focus(System.Windows.DependencyObject focus, bool askOld, bool askNew, bool forceToNullIfFailed) Unknown

  PresentationCore.dll!System.Windows.Input.KeyboardDevice.Focus(System.Windows.IInputElement element) Unknown

  PresentationCore.dll!System.Windows.UIElement.Focus() Unknown

  PresentationFramework.dll!System.Windows.Controls.ScrollViewer.OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e) Unknown

  PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown

  PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown

  PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args, System.Windows.RoutedEvent newEvent) Unknown

  PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown

  PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown

  PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown

  PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args) Unknown

  PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown

  PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) Unknown

  PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(nint hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) Unknown

  PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(nint hwnd, MS.Internal.Interop.WindowMessage msg, nint wParam, nint lParam, ref bool handled) Unknown

  PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Unknown

  WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(nint hwnd, int msg, nint wParam, nint lParam, ref bool handled) Unknown

  WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown

  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown

  WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown

  WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown

  WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(nint hwnd, int msg, nint wParam, nint lParam) Unknown

  [Native to Managed Transition]

  [Managed to Native Transition]

  WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Unknown

  PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Unknown

  PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Unknown

  EPD3.dll!EPD3.App.Main() Unknown



The ItemsSource contains models of type PractitionerHealthcareProviderCompanyDisplayModel. I Insert a new PractitionerHealthcareProviderCompanyDisplayModel model at the top of the list, so users can empty their selection like that

                ReferringPractitionersPractice = new ObservableCollection<PractitionerHealthcareProviderCompanyDisplayModel>(Enumerable.DistinctBy(ReferringPractitionersItemsSourceOrig, x => x.HealthcareProviderCompanyId));

                ReferringPractitionersPractice.Insert(0, new PractitionerHealthcareProviderCompanyDisplayModel() { HealthcareCompanyDisplayName = "" });





-- code of the customeditor starts here

using System.Reflection;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Data;


namespace EPD3.Infrastructure.SFControls.SfPropertyGrid.CustomEditors

{

    public class GenericComboBoxAdvPropertyGridEditor<T> : ITypeEditor

    {

        private ComboBoxAdv? _comboBox;

        private IEnumerable<T> _itemsSource;

        private T? _selectedItem;

        private string _displayMemberPath;

        private IValueConverter _valueConverter;


        public T? SelectedItem

        {

            get => _comboBox?.SelectedItem != null ? (T)_comboBox.SelectedItem : default;

            set

            {

                if (value == null)

                {

                    _comboBox?.SetValue(Selector.SelectedItemProperty, null);

                }

                else

                {

                    _comboBox?.SetValue(Selector.SelectedItemProperty, value);

                }

            }

        }


        public event SelectionChangedEventHandler? SelectionChanged;


        public GenericComboBoxAdvPropertyGridEditor(IEnumerable<T> itemsSource, T? selectedItem, string displayMemberPath, IValueConverter valueConverter)

        {

            _itemsSource = itemsSource;

            _selectedItem = selectedItem;

            _displayMemberPath = displayMemberPath;

            _valueConverter = valueConverter;

        }


        public object Create(PropertyInfo PropertyInfo)

        {

            _comboBox = new ComboBoxAdv

            {

                ItemsSource = _itemsSource,

                DisplayMemberPath = _displayMemberPath,

                SelectedValuePath = _displayMemberPath,

                SelectedItem = _selectedItem,

                IsEditable = true,

                AutoCompleteMode = AutoCompleteModes.Suggest,

                IsTabStop = true,


            };


            _comboBox.SelectionChanged += Combo_SelectionChanged;


            return _comboBox;

        }


        private void Combo_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            _comboBox!.GetBindingExpression(Selector.SelectedItemProperty)?.UpdateTarget();

            SelectionChanged?.Invoke(sender, e);

        }


        public object Create(PropertyDescriptor PropertyDescriptor)

        {

            return Create((PropertyInfo)null);

        }


        public void Attach(PropertyViewItem property, PropertyItem info)

        {

            if (_comboBox != null)

            {

                var binding = new Binding("Value")

                {

                    Mode = BindingMode.TwoWay,

                    Source = info,

                    Converter = _valueConverter,

                };

                BindingOperations.SetBinding(_comboBox, Selector.SelectedValueProperty, binding);

            }

        }


        public void Detach(PropertyViewItem property)

        {

        }


        public bool ShouldPropertyGridTryToHandleKeyDown(System.Windows.Input.Key key)

        {

            return false;

        }


        public void UpdateItemsSource(IEnumerable<T> newItemsSource)

        {

            if (_comboBox != null)

            {

                _comboBox.SetCurrentValue(ItemsControl.ItemsSourceProperty, newItemsSource);

            }

        }

    }

}

-- code of the customeditor ends here



7 Replies 1 reply marked as answer

RS Raghavendra Sudhakar Syncfusion Team April 13, 2023 02:20 PM UTC

Hi Van Strien,


We were unable to reproduce the issue in our sample. Please find the sample for your reference. 


Can you please provide the following details

* Can you please share the properties used in your PropertyGrid sample?
* If possible, please share the modified sample.


Regards,

Raghavendra S


Attachment: CustomPropertyEditor_76c5a153.zip


NV NM van Strien April 14, 2023 05:34 PM UTC

Dear Raghavendra ,


Thank you for the sample.  I have changed the code to the way I use it and the null error is there.

It seems to have to do with the ItemsSource to which I add an empty record. Perhaps that is wrong, but the error is not so clear to me.

Steps to reproduce:

1 extract the zip attached to this post

2 open the project in VS

3 run the project

4 select an item in the list


-> null error occurs


Attachment: CustomPropertyEditor_76c5a153_with_error_98d9fcd4.zip


NV NM van Strien April 14, 2023 07:01 PM UTC

I seen now that when I initialize the empty record like this, the error goes away


countryInfoCollection.Insert(0, new CountryInfo() { CountryName = "", Continent = ""}); // create an empty record at the top


I cannot oversee the implications, but perhaps the DisplayMemberPath property can be made nullable?


Kind regards,


Niels



RS Raghavendra Sudhakar Syncfusion Team April 20, 2023 02:26 PM UTC

Hi NM van Strien,


We have confirmed the reported scenario is a defect and logged a report for the reported scenario “NullReferenceException is thrown while selecting an item from ComboBox”. We will provide the fix in the Weekly NuGet release on 16th May 2023.


You can track the status of this defect using the following feedback link:   https://www.syncfusion.com/feedback/43021/nullreferenceexception-is-thrown-while-selecting-an-item-from-combobox

If you have any more specification replication procedures or a scenario to be tested, you can add it as a comment in the portal.


Please let us know if you need any further assistance.


Note: The provided feedback link is private, and you need to log in to view this feedback.



Marked as answer

BA BagavathiPerumal Apranandham Syncfusion Team May 16, 2023 08:48 AM UTC

Hi NM van Strien,


Sorry for the inconvenience,


We are still working on this issue with high priority. We will update the fix in our upcoming NuGet release which is scheduled on 23rd May 2023. We will appreciate your patience until then.



Regards,

Bagavathi Perumal A




NV NM van Strien May 16, 2023 08:50 AM UTC

Thank you for your continued support!



RS Raghavendra Sudhakar Syncfusion Team May 23, 2023 10:13 AM UTC

Hi NM van Strien

We have included the fix for the reported issue “NullReferenceException is thrown while selecting an item from ComboBox” in our Weekly NuGet release version 21.2.6 which is available for download ( https://www.nuget.org/).

We thank you for your support and appreciate your patience in waiting for this update. Please get in touch with us if you require any further assistance.


Regards,

Raghavendra S


Loader.
Live Chat Icon For mobile
Up arrow icon