I'm getting a null reference in the PresentationFramework on my LifeShaping filtering:
The stack trace isn't giving me much clue:
at System.Windows.Data.ListCollectionView.RestoreLiveShaping()
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.ShowDialog()
at MVVMSeaCores.AppWindowManager.ShowDialog(Object rootModel, Object context, IDictionary`2 settings)
That last line is the dialog call that shows the UX that holds the checkbox bound to ShowOnGraph
.
I'm setting the live-shaping like this, based off a boolean property "ShowOnGraph":
KPIBarsView = new CollectionViewSource { Source = KPIBars }.View;
KPIBarsView.Filter = FilterBars;
//grouping
if (KPIBarsView != null && KPIBarsView.CanGroup == true)
{
KPIBarsView.GroupDescriptions.Clear();
KPIBarsView.GroupDescriptions.Add(new PropertyGroupDescription("KPIViewModel.ContextViewModel"));
}
//Live Filtering
ICollectionViewLiveShaping KPIBarsViewLiveShaping = KPIBarsView as ICollectionViewLiveShaping;
if (KPIBarsViewLiveShaping.CanChangeLiveFiltering)
{
KPIBarsViewLiveShaping.LiveFilteringProperties.Add("ShowOnGraph");
KPIBarsViewLiveShaping.IsLiveFiltering = true;
}
Items are filtered as I'd expect when ShowOnGraph
is set to false. However, as soon as I try and unfilter anything with ShowOnGraph=true
I get this exception.
This is not a duplicate of "What's a null reference exception". I know what a null reference exception is. But in this case, the null reference is in the Presentation Framework, in System.Windows.Data. I've got no idea what's null, why (the list doesn't contain any null entries, the filter Property is a bool and cannot be null).
The null object isn't in my code, and isn't available for me to debug. All I get in the debugger is where in the dispatch it was when this occured. In one case, it's in the dialog that contains the list where I'm setting it to true:
There's nothing null.
I'll just make a button to set a ShowOnGraph=false, and see where the exception occurs there.
Edit: Yep, it occurs "nowhere". Just opens up on a blank "Break mode" page with no content or indication of where the error occurred.
Found a solution!
I was creating the view directly (rather than using the default view, because I in had two views driven from this collection:
KPIBarsView = new CollectionViewSource { Source = KPIBars }.View;
Which I did after reading this: http://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx
and the following SA questions:
- WPF Multiple CollectionView with different filters on same collection
- WPF CollectionViewSource Multiple Views?
However, I tried just creating a new collection, populating it with the same items and using:
KPIBarsView = CollectionViewSource.GetDefaultView(KPIBars);
solved the problem. Hope this is helpful for anyone else who stumbles upon it.
johnDisplayClass's comment is very helpful.
Something that worked for me: was if i also kept a member reference to each new CollectionViewSource as well as its CollectionView. This kept my live shaping and filtering working. Just this alone solved the same null ref that the OP was experiencing.
Another way to prevent this null exception is to set IsLiveSorting / IsLiveGrouping / IsLiveFiltering to false before the CollectionViewSource or CollectionView been garbage collected.
What I recommend is that you set yourself up to debug the Microsoft dll's from within your solution
https://msdn.microsoft.com/en-us/library/cc667410.aspx
Then make sure your debug settings have all of the possible exception types checked, then when you run your app again and break on the exception, you'll get a full stack trace that could help you work out the isse.
来源:https://stackoverflow.com/questions/37166747/object-reference-not-set-to-an-instance-of-an-object-in-presentationframework