title: NullReferenceException during Measure in WPF
body: <p>I'm getting a NullReferenceException thrown out of the bowels of WPF during a measure cycle.  When the application gets into this state, it usually just starts throwing this exception until the app dies.  Unfortunately, none of my code occures in the call stack, and there is no real identifying information about exactly what is causing this.  The problem is not readily reproducable, it just happens from time to time.</p>
  
  <p>Any ideas on how to go about trying to troubleshoot this?</p>
  
  <p>Here's the full dump of the exception:</p>
  
  <pre><code>Dispatch Error: System.NullReferenceException: Object reference not set to an instance of an object.
     at System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem, GeneratorState&amp;amp; state)
     at System.Windows.Controls.ItemContainerGenerator.Generator..ctor(ItemContainerGenerator factory, GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem)
     at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.StartAt(GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem)
     at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Page.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Control.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
     at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
     at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Controls.Border.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.Window.MeasureOverrideHelper(Size constraint)
     at System.Windows.Window.MeasureOverride(Size availableSize)
     at ActiproSoftware.Windows.Controls.Ribbon.RibbonWindow.MeasureOverride(Size availableSize)
     at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
     at System.Windows.UIElement.Measure(Size availableSize)
     at System.Windows.ContextLayoutManager.UpdateLayout()
     at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
     at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
     at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
     at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
     at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
     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)
  </code></pre>