title: Adding Button Click="...." causes "Object reference not set to an instance of an object" on startup
body: <p>Here's the code that I'm using to generate the window:</p>
  
  <pre><code>&lt;Grid x:Name="LayoutRoot"&gt;
      &lt;TreeView DataContext="{Binding ElementName=UserControl, Mode=OneWay}"
          ....
          ItemsSource="{Binding UrlList}" ItemContainerStyle="{DynamicResource TreeViewItemStyle}" &gt;
          &lt;TreeView.Resources&gt;
              &lt;HierarchicalDataTemplate DataType="{x:Type local:UrlList}"&gt;
                  &lt;Grid Margin="-15,0,0,0"&gt;                       
                      &lt;Expander&gt;
                          &lt;Expander.Header&gt;
                              ....
                          &lt;/Expander.Header&gt;
                          &lt;StackPanel&gt;
                              ....
                              &lt;DataGrid ItemsSource="{Binding List}" Margin="0,10,0,0" AutoGenerateColumns="False"&gt;
                                  &lt;DataGrid.Columns&gt;
                                      ....
                                      &lt;DataGridTemplateColumn&gt;
                                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                                              &lt;DataTemplate&gt;
                                                  &lt;Button Content="..."  Click="EditUri_Click"/&gt;
                                              &lt;/DataTemplate&gt;                                             
                                          &lt;/DataGridTemplateColumn.CellTemplate&gt;                                          
                                      &lt;/DataGridTemplateColumn&gt;
                                  &lt;/DataGrid.Columns&gt;
                              &lt;/DataGrid&gt;
                          &lt;/StackPanel&gt;       
                      &lt;/Expander&gt;
                  &lt;/Grid&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/TreeView.Resources&gt;
      &lt;/TreeView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>when I use <code>&lt;Button Content="..." /&gt;</code>, the application starts normally, when I use <code>&lt;Button Content="..."  Click="EditUri_Click"/&gt;</code> I get following exception:</p>
  
  <pre><code>    System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    Source=MyApp
    StackTrace:
         at UrlListInfoPanel.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) in ...\UrlListInfoPanel.xaml:line 152[this is where the button is]
         at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
         at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
         at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
         at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
         at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
         at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
         at System.Windows.FrameworkElement.ApplyTemplate()
         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.TreeViewItem.MeasureOverride(Size constraint)
         at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
         at System.Windows.UIElement.Measure(Size availableSize)
         at System.Windows.Controls.StackPanel.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.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 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.Documents.AdornerDecorator.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.Border.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.Window.MeasureOverrideHelper(Size constraint)
         at System.Windows.Window.MeasureOverride(Size availableSize)
         at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
         at System.Windows.UIElement.Measure(Size availableSize)
         at System.Windows.Interop.HwndSource.SetLayoutSize()
         at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
         at System.Windows.Window.SetRootVisualAndUpdateSTC()
         at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
         at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
         at System.Windows.Window.ShowHelper(Object booleanBox)
         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)
         at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
         at System.Windows.Threading.DispatcherOperation.InvokeImpl()
         at System.Threading.ExecutionContext.runTryCode(Object userData)
         at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext 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&amp; handled)
         at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
         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)
         at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
         at System.Windows.Threading.Dispatcher.InvokeImpl(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&amp; msg)
         at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
         at System.Windows.Application.RunInternal(Window window)
         at System.Windows.Application.Run()
         at BookmarkWizV2.App.Main() in App.g.cs:line 0
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
  </code></pre>
  
  <p>Where does this come from, and how do I fix this?</p>