question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException is thrown in the System.Windows.StyleHelper.GetExpression method

See original GitHub issue
  • .NET Core Version: .NET 7.0 SDK (v7.0.100-rc.2)
  • Windows version: Version 21h1 (OS Build 19043.2130)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No
  • Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc…)?: No

Problem description: An attempt to call the BindingOperations.GetBindingExpression method in .NET 7.0 leads to Null Reference Exception.

Actual behavior:

NullRerferenceException is thrown right at the moment when the following code is executed:

public App() {
    var viewSource = new CollectionViewSource();
    var be = BindingOperations.GetBindingExpression(viewSource, CollectionViewSource.SourceProperty);
}

The callstack indicates that the error is thrown in the System.Windows.StyleHelper.GetExpression method:

Callstack:

PresentationFramework.dll!System.Windows.StyleHelper.GetExpression(System.Windows.DependencyObject d, System.Windows.DependencyProperty dp)
PresentationFramework.dll!System.Windows.Data.BindingOperations.GetBindingExpressionBase(System.Windows.DependencyObject target, System.Windows.DependencyProperty dp)
PresentationFramework.dll!System.Windows.Data.BindingOperations.GetBindingExpression(System.Windows.DependencyObject target, System.Windows.DependencyProperty dp)
WpfApp1.dll!WpfApp1.App.App() Line 18
WpfApp1.dll!WpfApp1.App.Main()

Expected behavior:

No errors should be thrown.

Minimal repro:

Download and run the project. The error should be thrown right at the startup.

The project and screencast in the attachment illustrate the problematic behavior.

dxSample.zip screencast.mp4

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
miloushcommented, Oct 14, 2022

@batzen this commit https://github.com/dotnet/wpf/commit/5769bbfd15d34e2ec98f44406862d831a514a895 broke it as static constructors no longer run

(to elaborate, FrameworkElement.cctor -> Style.cctor -> StyleHelper.RegisterAlternateExpressionStorage no longer runs in StyleHelper.GetExpression -> Helper.DowncastToFEorFCE)

In this very case one could argue that RegisterAlternateExpressionStorage should be called in StyleHelper cctor rather than Style one, but I am little worried about other side effects that PR might have caused by skipping static cctors of types.

2reactions
danlyons-softekcommented, Nov 9, 2022

@miloush Apologies for missing that detail - yes and no 😄

It looks like in all the cases I ran into issues, the code in question is assigning a value to Source on the CollectionViewSource. There are no direct references to BindingOperations.GetBindingExpression I can see.

However, according to the stack traces I see, the Source assignment ultimately triggers a call to StyleHelper.GetExpression via BindingOperations.GetBindingExpressionBase anyway.

Example:

public TreeNode()
{
    Children = new ObservableCollection<TreeNode>();
    ChildrenSource = new CollectionViewSource { Source = Children };
    ChildrenSource.SortDescriptions.Add(new SortDescription(nameof(Name), ListSortDirection.Ascending));
    ChildrenSource.IsLiveSortingRequested = true;
}
System.NullReferenceException : Object reference not set to an instance of an object.
   at System.Windows.StyleHelper.GetExpression(DependencyObject d, DependencyProperty dp)
   at System.Windows.Data.BindingOperations.GetBindingExpressionBase(DependencyObject target, DependencyProperty dp)
   at System.Windows.Data.CollectionViewSource.<EnsureView>b__103_0(Object x)
   at MS.Internal.Data.ViewManager.EnsureCollectionRecord(Object collection, Func`2 GetSourceItem)
   at MS.Internal.Data.ViewManager.GetExistingView(Object collection, CollectionViewSource cvs, Type collectionViewType, Func`2 GetSourceItem)
   at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.EnsureView(Object source, Type collectionViewType)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   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.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at TreeNode..ctor()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid crash on NullReferenceException in StyleHelper. ...
In my case the exception was thrown when a StaticResource style was used in a template within a child window; the same template...
Read more >
How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ......
Read more >
NullReferenceException Class (System)
A NullReferenceException exception is thrown by a method that is passed null . Some methods validate the arguments that are passed to them....
Read more >
How to solve System.NullReferenceException?
A NullReferenceException typically occurs when you try to access a member or call a method on an object that is currently null. In...
Read more >
Receiving Runtime Error System.NullReferenceException ...
I am receiving a runtime error message...... System.NullReferenceException: 'Object reference not set to an instance of an object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found