5
votes

I'm getting a null reference in the PresentationFramework on my LifeShaping filtering:

enter image description here

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 ShowOnGraphis 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:

enter image description here

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.

3
This is not a duplicate of "What's a null reference"... I know what a null reference exception is. - Joe
Well, have you tried debugging it and looking in your locals window to see what might be null that shouldn't be null? Visual Studio is a very powerful debugging tool (assuming you're using VS). Learn how to make use of it. - rory.ap
I can't debug System.Windows.Data, where the error is occurring. The debugger provides no information on what's null or not. - Joe
Well, then I don't know what you expect to get by posting your question here. We don't have any more insight into how System.Windows.Data classes work than you do. Have you tried submitting this as a bug to Microsoft? Did you do any research into this before you posted it here to see if anyone else has had this issue? - rory.ap
I'd like to see if I'm not missing something obvious, or if anyone else has had similar issues or knows any case where this is expected to happen before jumping right to submitting to Microsoft. Most of the time I'm at fault, rather than it being a bug in the system. I've done a fair bit of google searching and not found anything too similar. Isn't this a programming question and answer site? Either way, this is not a duplicate. Closest I could find is here: stackoverflow.com/questions/1553465/… but that's not filtering - Joe

3 Answers

2
votes

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:

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.

2
votes

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.

0
votes

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.