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.
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 inStyleHelper.GetExpression
->Helper.DowncastToFEorFCE
)In this very case one could argue that
RegisterAlternateExpressionStorage
should be called inStyleHelper
cctor rather thanStyle
one, but I am little worried about other side effects that PR might have caused by skipping static cctors of types.@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 theCollectionViewSource
. There are no direct references toBindingOperations.GetBindingExpression
I can see.However, according to the stack traces I see, the
Source
assignment ultimately triggers a call toStyleHelper.GetExpression
viaBindingOperations.GetBindingExpressionBase
anyway.Example: