title: Reference instance when MainWindow is not the startup window
body: <p>I am doing some application for college and I have successfully been able to show and collapse usercontrols in my application, but when the window I am trying to do it is not the one that starts when the application starts, it just doesn't work.</p>
  
  <p>I have a mouse event that works when I click on one of the labels (acting as a menu):</p>
  
  <pre><code>class MouseEvents
      {        public void MouseClick(object sender)
          {
              //creating an instance of MainWindow to be able to reference the labels and user controls
              MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
              List&lt;Label&gt; menuDown = new List&lt;Label&gt; { mainWindow.toolAddPlayer, mainWindow.toolSearchPlayer, mainWindow.toolAddMember, mainWindow.toolSearchMember, mainWindow.toolEditPass, mainWindow.toolEmailList };
              List&lt;UserControl&gt; userDown = new List&lt;UserControl&gt; { mainWindow.addPlayer, mainWindow.searchPlayer, mainWindow.addMember, mainWindow.searchMember, mainWindow.editPassword, mainWindow.EmailList };
              var menuItem = (Label)sender;
              for (int i = 0; i &lt; 6; i++)
              {
                  if (menuItem.Content == menuDown[i].Content)
                  {
                      HideUserControls();
                      userDown[i].Visibility = Visibility.Visible;
                  }
              }
          }
  
          public void HideUserControls()
          {
              MainWindow usingControls = (MainWindow)Application.Current.MainWindow;
              usingControls.addPlayer.Visibility = Visibility.Collapsed;
              usingControls.searchPlayer.Visibility = Visibility.Collapsed;
              usingControls.addMember.Visibility = Visibility.Collapsed;
              usingControls.searchMember.Visibility = Visibility.Collapsed;
              usingControls.editPassword.Visibility = Visibility.Collapsed;
              usingControls.EmailList.Visibility = Visibility.Collapsed;
          }
      }
  </code></pre>
  
  <p>On the window xaml (MainWindow.xaml) where I want my usercontrols to appear or disappear, I have this code </p>
  
  <pre><code>        &lt;StackPanel DockPanel.Dock="Bottom" Height="440" Width="590"&gt;
              &lt;local:AddPlayer x:Name="addPlayer" Visibility="Collapsed"/&gt;
              &lt;local:SearchPlayer x:Name="searchPlayer" Visibility="Collapsed"/&gt;
              &lt;local:AddMember x:Name="addMember" Visibility="Collapsed"/&gt;
              &lt;local:SearchMember x:Name="searchMember" Visibility="Collapsed"/&gt;
              &lt;local:EditPassword x:Name="editPassword" Visibility="Collapsed"/&gt;
              &lt;local:EmailList x:Name="EmailList" Visibility="Collapsed"/&gt;
          &lt;/StackPanel&gt;
  </code></pre>
  
  <p>And it works perfectly. The problem is when I want to add a window to log in the application (this LogIn window will then load the MainWindow.xaml when I click the LogIn button). I changed my App.xaml to update the StartupUri to LogIn.xaml</p>
  
  <pre><code>&lt;Application x:Class="SimplyRugby_2._0.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:local="clr-namespace:SimplyRugby_2._0"
               StartupUri="LogIn.xaml"&gt;
      &lt;Application.Resources&gt;
  
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>So the application starts as it should, on the LogIn window. The problem comes when I try to click on the menu on the MainWindow window. </p>
  
  <p>I get a reference not to an instance of an object error. I have been checking around on StackOverflow and google but I really cannot seem to find the solution. I don't know how to reference it. I did it when MainWindow was the window that starts when I initialize the app, but if it is not the first window, it just gives me the error</p>
  
  <pre><code>System.NullReferenceException: 'Object reference not set to an instance of an object.'
  
  mainWindow was null.
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Thank you so much in advance</p>