Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertyGrid.OnPropertyValueSet: NullReferenceException (version 2) #11768

Closed
ulfemsoy opened this issue Jul 29, 2024 · 4 comments · Fixed by #11787
Closed

PropertyGrid.OnPropertyValueSet: NullReferenceException (version 2) #11768

ulfemsoy opened this issue Jul 29, 2024 · 4 comments · Fixed by #11787
Assignees
Labels
area-controls-PropertyGrid PropertyGrid and editor related issues area-NRT 🚧 work in progress Work that is current in progress

Comments

@ulfemsoy
Copy link

          @ulfemsoy there is not enough information for us to diagnose this issue. When you have more information on how you encountered this issue, please reopen this bug or create a new one with the details then we'd be happy to help!

Originally posted by @merriemcgaw in #11495 (comment)

ISSUE:
Please refer to line 2946 in PropertyGrid.cs:
Type propertyType = changedItem.PropertyDescriptor!.PropertyType;
(

Type propertyType = changedItem.PropertyDescriptor!.PropertyType;
)

The value of "changedItem.PropertyDescriptor" can be null, and will raise a NullReferenceException when requesting the property "PropertyType".

"PropertyDescriptor" is null when "changedItem" is a CategoryGridEntry, SingleSelectRootGridEntry or ArrayElementGridEntry.

Please perform a null test (changedItem.PropertyDescriptor is not null) before requesting the property type.

@elachlan elachlan added area-controls-PropertyGrid PropertyGrid and editor related issues untriaged The team needs to look at this issue in the next triage area-NRT labels Jul 29, 2024
@elachlan
Copy link
Contributor

@Olina-Zhang can your team please test this to see if it is a preview regression?

@Olina-Zhang
Copy link
Member

@Olina-Zhang can your team please test this to see if it is a preview regression?

We cannot reproduce it with limited information. From originally posted in GH issue #11495, repro in .NET 6.0, it doesn't look like a preview regression. @ulfemsoy can you please test if it reproduces or not in .NET 8.0 or .NET 9.0?

@elachlan elachlan added the waiting-author-feedback The team requires more information from the author label Jul 30, 2024
@ulfemsoy
Copy link
Author

@Olina-Zhang, it is an old issue that happens now and then. In 2 years the specific NullReferenceException has only been raised twice for Asian users. The program is written for .NET 6, so it is hard to reproduce for .NET 8/9.

It would be nice if you could modify the method to:

internal void OnPropertyValueSet(GridItem changedItem, object? oldValue)
    {
        OnPropertyValueChanged(new PropertyValueChangedEventArgs(changedItem, oldValue));

        if (changedItem is null)
        {
            return;
        }

        // Announce the property value change like standalone combobox control do: "[something] selected".
        bool dropDown = false;

        PropertyDescriptor propertyDescriptor = changedItem.PropertyDescriptor;

        if(propertyDescriptor is not null)
        {
                Type propertyType = propertyDescriptor.PropertyType;
                var editor = (UITypeEditor?)TypeDescriptor.GetEditor(propertyType, typeof(UITypeEditor));
                if (editor is not null)
                {
                    dropDown = editor.GetEditStyle() == UITypeEditorEditStyle.DropDown;
                }
                else
                {
                    if (changedItem is GridEntry gridEntry && gridEntry.Enumerable)
                    {
                        dropDown = true;
                    }
                }
        }

        if (IsAccessibilityObjectCreated && dropDown && (_gridView is not null) && !_gridView.DropDownVisible)
        {
            AccessibilityObject.RaiseAutomationNotification(
                Automation.AutomationNotificationKind.ActionCompleted,
                Automation.AutomationNotificationProcessing.All,
                string.Format(SR.PropertyGridPropertyValueSelectedFormat, changedItem.Value));
        }
    }

Please notice the null test of "if(propertyDescriptor is not null)", and the additional "(_gridView is not null)" statement. This will avoid any future NullReferenceExceptions.

@dotnet-policy-service dotnet-policy-service bot removed the waiting-author-feedback The team requires more information from the author label Jul 30, 2024
@Olina-Zhang
Copy link
Member

@ulfemsoy Thanks for your information. Let's wait for our engineering team's triage and review.

@JeremyKuhne JeremyKuhne self-assigned this Jul 31, 2024
JeremyKuhne added a commit to JeremyKuhne/winforms that referenced this issue Jul 31, 2024
It is possible to not have a PropertyDescriptor, we should handle that case.

Fixes dotnet#11768
@dotnet-policy-service dotnet-policy-service bot added the 🚧 work in progress Work that is current in progress label Jul 31, 2024
@elachlan elachlan removed the untriaged The team needs to look at this issue in the next triage label Jul 31, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 31, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-PropertyGrid PropertyGrid and editor related issues area-NRT 🚧 work in progress Work that is current in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants