title: System.NullReferenceException thrown when showing a Window
body: <p>I am working on WPF and using Devexpress tool for GUI,  </p>
  
  <p>Whenever I debug my program it always generate exception of Null object reference and pointed to <code>W1.SHOW();</code>  where I created a window2.  </p>
  
  <p>Can someone explain why?</p>
  
  <p>The program works fine, but as I click button then window2 appears, in that window I showed two tabs.</p>
  
  <p>Window2 also shows these two tabs but when I click at one of these tabs then it generates an exception. I don't know why it is doing so :(</p>
  
  <pre><code>// using statements omitted for brevity...
  
  namespace WpfApplication1
  {
  
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
                     CreateNavBarControl();
          }
  
       // When user will click NewDocumentButton, following function will get execute.   
          private void NewDocumentButton_Click(object sender, RoutedEventArgs e)
          {
              Window2 W1 = null;
              //obj.ShowDialog();
              //obj.Close();
  
              if (W1 == null)
              {
                  ***W1 = new Window2();
                  W1.Show();***
  
              }
  
  
          }
  
          }
      }
  </code></pre>
  
  <p>Window2 - Code</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Shapes;
  
  namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for Window2.xaml
      /// &lt;/summary&gt;
      public partial class Window2 : Window
      {
          public Window2()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
  
          }
  
  
      }
  }
  </code></pre>
  
  <p>Window1 - XAML</p>
  
  <pre><code>    &lt;Window x:Class="WpfApplication1.MainWindow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="News Room Computer System" Height="196" Width="331"  xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
              xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar"
             xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
              dx:ThemeManager.ThemeName="MetropolisDark" 
              xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
              Loaded="Window_Loaded" DataContext="{Binding}" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" Background="Black"&gt;
          &lt;Grid&gt;
  
              &lt;DockPanel x:Name="dockPanel" Margin="5,25,0,0"   Width="86" LastChildFill="False" HorizontalAlignment="Left"&gt;
  
              &lt;/DockPanel&gt;
              &lt;dxlc:LayoutGroup Margin="5,0,12,138" Name="layoutGroup1"&gt;
                  &lt;Button Name="NewDocumentButton" Height="19" Width="45"   
                          VerticalAlignment="Top" Content="New" Click="NewDocumentButton_Click"  /&gt;
              &lt;/dxlc:LayoutGroup&gt;
          &lt;/Grid&gt;
  
  
      &lt;/Window&gt;
  </code></pre>
  
  <p>Window 2 - XAML</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window2"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="Window2" Height="300" Width="300" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" Loaded="Window_Loaded" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"&gt;
      &lt;Grid&gt;
          &lt;dxlc:LayoutGroup Header="LayoutGroup" Name="layoutGroup2" View="Tabs" Width="202" Height="175" Margin="38,20,38,66" ContextMenuClosing="layoutGroup2_ContextMenuClosing" ContextMenuOpening="layoutGroup2_ContextMenuClosing" DragEnter="layoutGroup2_DragEnter" MeasureSelectedTabChildOnly="True" SelectedTabIndex="2" DragScrolling="True"&gt;
              &lt;dxlc:LayoutGroup Header="ass" Name="layoutGroup3"  Height="116" Width="153" /&gt;
              &lt;dxlc:LayoutControl Height="117"  HorizontalAlignment="Left" dxlc:LayoutControl.TabHeader="va" Name="layoutControl1"  VerticalAlignment="Top" Width="164"&gt;
                  &lt;dxlc:LayoutItem Label="layoutItem1" Name="layoutItem1"&gt;
                      &lt;dxe:TextEdit Name="textEdit1" /&gt;
                  &lt;/dxlc:LayoutItem&gt;
              &lt;/dxlc:LayoutControl&gt;
          &lt;/dxlc:LayoutGroup&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>//---- STACKTRACE ------------------</p>
  
  <pre><code>System.NullReferenceException was unhandled
    Message=Object reference not set to an instance of an object.
    Source=DevExpress.Xpf.LayoutControl.v12.2
    StackTrace:
         at DevExpress.Xpf.LayoutControl.LayoutGroup.OnTabControlSelectionChanged()
         at DevExpress.Xpf.LayoutControl.LayoutGroup.OnTabControlSelectionChanged(Object sender, TabControlSelectionChangedEventArgs e)
         at DevExpress.Xpf.Core.DXTabControl.RaiseSelectionChanged(Int32 oldSelectedIndex)
         at DevExpress.Xpf.Core.DXTabControl.OnSelectedIndexPropertyChanged(Int32 oldValue)
         at DevExpress.Xpf.Core.DXTabControl.&lt;.cctor&gt;b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
         at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
         at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
         at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
         at DevExpress.Xpf.Core.DXTabControl.OnSelectedItemPropertyChanged(Object oldValue)
         at DevExpress.Xpf.Core.DXTabControl.&lt;.cctor&gt;b__1(DependencyObject d, DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
         at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
         at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
         at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
         at DevExpress.Xpf.Core.DXTabControl.OnSelectedTabItemPropertyChanged(DXTabItem oldValue)
         at DevExpress.Xpf.Core.DXTabControl.&lt;.cctor&gt;b__2(DependencyObject d, DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
         at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
         at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
         at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
         at DevExpress.Xpf.Core.DXTabControl.DevExpress.Xpf.Core.ITabControl.SelectItem(DXTabItem item)
         at DevExpress.Xpf.Core.DXTabItem.OnIsSelectedPropertyChanged()
         at DevExpress.Xpf.Core.DXTabItem.&lt;.cctor&gt;b__2(DependencyObject d, DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
         at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
         at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry&amp; newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
         at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
         at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
         at DevExpress.Xpf.Core.DXTabItem.OnMouseLeftButtonClick(MouseButtonEventArgs e)
         at DevExpress.Xpf.Core.DXTabItem.OnMouseLeftButtonDown(MouseButtonEventArgs e)
         at System.Windows.UIElement.OnMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
         at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
         at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
         at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
         at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
         at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
         at System.Windows.Input.InputManager.ProcessStagingArea()
         at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
         at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
         at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
         at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at System.Windows.Interop.HwndSource.InputFilterMessage(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.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.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
         at System.Windows.Threading.Dispatcher.Run()
         at System.Windows.Application.RunDispatcher(Object ignore)
         at System.Windows.Application.RunInternal(Window window)
         at System.Windows.Application.Run(Window window)
         at System.Windows.Application.Run()
         at WpfApplication1.App.Main() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\WpfApplication1\WpfApplication1\obj\x86\Debug\App.g.cs:line 0
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    Detail:  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()  InnerException: 
  </code></pre>