Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid crash on NullReferenceException in StyleHelper.OnBindingValueInTemplateChanged

My application had crashed several times with the following stack trace:

An uncatched exception was thrown by application: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.StyleHelper.OnBindingValueInTemplateChanged(Object sender, BindingValueChangedEventArgs e)
   at System.Windows.Data.BindingExpressionBase.ChangeValue(Object newValue, Boolean notify)
   at System.Windows.Data.MultiBindingExpression.TransferValue()
   at System.Windows.Data.MultiBindingExpression.Transfer()
   at System.Windows.Data.MultiBindingExpression.UpdateTarget(Boolean includeInnerBindings)
   at System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean lastChance)
   at System.Windows.Data.MultiBindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

The problem is that this happens only in production environment and occurs rarely - about 2-3 times in the past month. So I can no possibility to debug it and I have only the logs. The crash occurred after the system navigated to another state (I see in the log that the next state view and view model are both loaded). The stack trace doesn't point at the specific place.

I did some investigation of this issue, and here're my findings:

The source code of the StyleHelper class contains a comment above this method: "Is Invoked when a binding in a condition of a data trigger changes its value". And in the call stack written in log I see MultiBindingExpression so I guess this issue is related to MultiDataTrigger. The only place we use MultiDataTrigger in our xamls (related to this particular state) is this style. It also looks (from the OnBindingValueInTemplateChanged code) that the only candidates to be null are a sender (that is BindingExpression) or the TargetElement of this expression (because another variables have a null-check and if there was a crash in any method called from this method so it had been writted to the call stack). I really don't understand in which scenario two of these friends could be null...

Did anyone face the same exception before and can advise how it can happen? Or any suggestions how I can avoid the application to crash?

like image 701
Eyjafjallajokull Avatar asked Apr 25 '26 11:04

Eyjafjallajokull


1 Answers

I have faced this problem too. The excpeption was reproduced stably. In my case I had ToggleButton with default style located in Generic.xaml file. In one UserControl I had ToggleButton with OverrideDefaultStyle="True". After removing OverrideDefaultStyle="True" the descriped exception no longer occurred.

like image 184
Egor Novikov Avatar answered Apr 28 '26 02:04

Egor Novikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!