Declined
Last Updated: 05 Mar 2021 09:51 by ADMIN
Jong
Created on: 02 Mar 2021 15:57
Category: UI for WinForms
Type: Bug Report
0
PropertyGrid throws a null reference exception when an object property is not initialized

Hello, I encountered a bug with PropertyGrid when an object property is null.

But it only happens when the object has a simple property, a simple list, and a complex list with a custom control.

I'm attaching a sample project.

To reproduce this bug, run the project and hit the sort button or enter any search strings.

3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 05 Mar 2021 09:51

Hello, Jong,

The default PropertyGridValueElement has the following implementation for the Synchronize method: 
        public virtual void Synchronize()
        {
            this.Text = ((PropertyGridItem)VisualItem.Data).FormattedValue;
        }

This covers the cases when the data item is not initialized and can be null. If you use a similar approach for synchronization in the custom AutocompletePropertyGridValueElement that you use, sorting and searching are not expected to throw any exceptions. You should think about covering such cases when the default RadPropertyGrid's implementation is customized. 

I believe that the provided information sounds reasonable to you. Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Jong
Posted on: 03 Mar 2021 15:43

Hello Dess,

Thank you for your reply.

As I noted in the title, this happens only when a property value is not initialized or null.

And your improvement is a workaround to the property initialization, because (item.Instance as TestObject).JoinedTags + string.Empty returns an empty string when JoinedTags is null.

I think even if the property value is null, sorting or searching should throw a null reference exception? 

ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 03 Mar 2021 09:25
Hello, Jong,
 
The provided sample project is greatly appreciated. It seems that the issue is reproducible only when using the custom PropertyGridItemElement that you have in the project. 

After further investigation, I have noticed that the custom AutocompletePropertyGridValueElement and its Synchronize method leads to the error:
    public class AutocompletePropertyGridValueElement : PropertyGridValueElement
    {
        private StackLayoutElement stackPanel = null;
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
            stackPanel = new StackLayoutElement();
            stackPanel.StretchHorizontally = true;

            RadAutoCompleteBoxElement radAutoCompleteBox = new RadAutoCompleteBoxElement();
            stackPanel.Children.Add(radAutoCompleteBox);
            this.Children.Add(stackPanel);
        }
        public override void Synchronize()
        {
            PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
            foreach (RadAutoCompleteBoxElement radAutoCompleteBox in stackPanel.Children)
                radAutoCompleteBox.Text = (item.Instance as TestObject).JoinedTags;
        }
    }

I have made the following improvement in the custom implementation and it seems to work properly:  

        public override void Synchronize()
        {
            PropertyGridItem item = this.VisualItem.Data as PropertyGridItem;
            foreach (RadAutoCompleteBoxElement radAutoCompleteBox in stackPanel.Children)
            {
                radAutoCompleteBox.Text = (item.Instance as TestObject).JoinedTags + string.Empty;
            }
        }

Could you please give it a try on your end and see whether it will work for your scenario?

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Attached Files: