title: App.xaml Object Reference Not Set To An Instance Of An Object
body: <p>I recently started getting an error in my App.xaml after installing Blend 4, all my ViewModels now show an error of "Object Reference Not Set To An Instance Of An Object". I uninstalled Blend 4 and the errors went away for a few hours and are now back. </p>
  
  <p>I've checked out every commit I've made till the beginning of the project now and the problem still persists, I am completely baffled. What is going on here? This is causing my windows to throw errors in design mode about being unable to locate resources.</p>
  
  <p>Edit:An Update: Every few times I build the application it changes, this time it changed after changing the binding on a button on a UserControl. Before it was UserListViewModel and SettingViewModel. Now it's SettingsViewModel and MainScreenViewModel.</p>
  
  <p>App.xaml :</p>
  
  <pre><code>&lt;Application x:Class="HelpScoutMetrics.App"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:vm="clr-namespace:HelpScoutMetrics.ViewModel"&gt;
  &lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;vm:MainScreenViewModel x:Key="MainScreenViewModel" /&gt;
          &lt;vm:SettingsViewModel x:Key="SettingsViewModel" /&gt;
          &lt;vm:UserListViewModel x:Key="UserListViewModel" /&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/FlatButton.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>ViewModels:</strong></p>
  
  <pre><code>public class MainScreenViewModel : ViewModelBase
  {
      public MainScreenViewModel()
      {
          QuickStatistics = new QuickStats();
          QuickStatistics.UserQuickStats = new ObservableCollection&lt;UserQuickStat&gt;();
          QuickStatistics.UserQuickStats.Add(new UserQuickStat() { Name = "Test", TotalConversations = 93, TotalReplies = 57 });
          ApplicationData.MainViewModel = this; // Temp for debug of issues
      }
  
      public static Logger logger = LogManager.GetLogger("MainScreenViewModel");
  
      public MainWindow Window { get; set; }
  
      private UserReport m_UserOverall;
      public UserReport UserOverall
      {
          get { return m_UserOverall; }
          set { m_UserOverall = value; RaisePropertyChanged("UserOverall"); }
      }
  
      private QuickStats m_QuickStatistics;
      public QuickStats QuickStatistics
      {
          get { return m_QuickStatistics; }
          set { m_QuickStatistics = value; RaisePropertyChanged("QuickStatistics"); }
      }
  
      private DateTime m_SelectedDate;
      public DateTime SelectedDate
      {
          get { return m_SelectedDate; }
          set { m_SelectedDate = value; RaisePropertyChanged("SelectedDate"); }
      }
  
      private ObservableCollection&lt;DateTime&gt; m_SelectedDates;
      public ObservableCollection&lt;DateTime&gt; SelectedDates
      {
          get { return m_SelectedDates; }
          set { m_SelectedDates = value; RaisePropertyChanged("SelectedDates"); }
      }
  
      private bool m_EnableLoadQuickStatsButton;
      public bool EnableLoadQuickStatsButton
      {
          get { return m_EnableLoadQuickStatsButton; }
          set { m_EnableLoadQuickStatsButton = value; RaisePropertyChanged("EnableLoadQuickStatsButton"); }
      }
  
      private bool m_EnableLoadQuickStatsButtonTest = false;
      public bool EnableLoadQuickStatsButtonTest
      {
          get { return m_EnableLoadQuickStatsButtonTest; }
          set { m_EnableLoadQuickStatsButtonTest = value; RaisePropertyChanged("EnableLoadQuickStatsButtonTest"); }
      }
  
      public void NewUser()
      {
          QuickStatistics.UserQuickStats.Add(new UserQuickStat() { Name = "Test2", TotalConversations = 953, TotalReplies = 577 });
      }
  
      public void OpenSettings()
      {
          if(Window.SettingsFlyout.IsOpen)
          {
              Window.SettingsFlyout.IsOpen = false;
              logger.Log(LogLevel.Info, "Closed Settings Flyout");
          }
          else
          {
              SettingsViewModel viewModel = Window.SettingsFlyout.DataContext as SettingsViewModel;
              viewModel.MainWindow = Window;
              viewModel.LoadSettings();
              Window.SettingsFlyout.IsOpen = true;
              logger.Log(LogLevel.Info, "Opened Settings Flyout");
          }
      }
  
      public void OpenUsersList()
      {
          if(Window.UserListFlyout.IsOpen)
          {
              Window.UserListFlyout.IsOpen = false;
              logger.Log(LogLevel.Info, "Closed Users List Flyout");
          }
          else
          {
              UserListViewModel viewModel = Window.UserListFlyout.DataContext as UserListViewModel;
              UserListView userListView = Window.UserListFlyoutView;
              viewModel.UserListView = userListView;
              viewModel.MainWindow = Window;
              viewModel.SetupFreshViewModel();
  
              Window.UserListFlyout.IsOpen = true;
              logger.Log(LogLevel.Info, "Opened Users List Flyout");
  
          }
      }
  
      public void OpenLogWindow()
      {
          int count = ApplicationData.MainLogEntries.LogEvents.Count;
          NLogViewerView window = new NLogViewerView();
          window.Show();
          logger.Log(LogLevel.Info, "Opened Log Window");
          EnableLoadQuickStatsButtonTest = true;
      }
  
      public void RefreshView()// Temp for debug of issues
      {
          foreach (string name in MiscMethods.GetPropertyNames(this))
          {
              RaisePropertyChanged(name);
          }
      }
  }
  
  public class SettingsViewModel : ViewModelBase
  {
      public SettingsViewModel()
      {
          SettingsWindowLogic.LoadSettings(this);
      }
  
      private static Logger logger = LogManager.GetLogger("SettingsViewModel");
  
      public MainWindow MainWindow { get; set; }
  
      private string m_APIKey;
      public string APIKey
      {
          get { return m_APIKey; }
          set 
          { 
              m_APIKey = value; TriedToValidateKey = false; 
              KeyValidationButtonText = "Verify Key";
              VerifyButtonBackground = new SolidColorBrush(Color.FromArgb(255, 213, 213, 213));
              ApplicationData.ApplicationSettings.ValidAPIKeyExists = false;
              RaisePropertyChanged("APIKey"); 
          }
      }
  
      private bool m_SaveAPIKey = true;
      public bool SaveAPIKey
      {
          get { return m_SaveAPIKey; }
          set { m_SaveAPIKey = value; RaisePropertyChanged("SaveAPIKey"); }
      }
  
      /*====================================================================
       *             Key Validation &amp; Button
       * ==================================================================*/
  
      private bool m_ValidKey;
      public bool ValidKey
      {
          get { return m_ValidKey; }
          set 
          { 
              m_ValidKey = value;
              if(value)
              {
                  KeyValidationButtonText = "Valid!";
                  VerifyButtonBackground = new SolidColorBrush(Color.FromArgb(255, 18, 145, 47));
                  ApplicationData.ApplicationSettings.ValidAPIKeyExists = true;
              }
              else
              {
                  KeyValidationButtonText = "Invalid";
                  VerifyButtonBackground = new SolidColorBrush(Color.FromArgb(255, 153, 18, 18));
                  ApplicationData.ApplicationSettings.ValidAPIKeyExists = false;
              }
              RaisePropertyChanged("ValidKey"); 
          }
      }
  
      //Will be true when the eky is in process of verification
      private bool m_CurrentlyVerifyingKey;
      public bool CurrentlyVerifyingKey
      {
          get { return m_CurrentlyVerifyingKey; }
          set 
          {
              if (value)
              {
                  KeyValidationButtonText = "Verifying...";
              }
              m_CurrentlyVerifyingKey = value; 
              RaisePropertyChanged("CurrentlyVerifyingKey");
          }
      }
  
      private bool m_TriedToValidateKey;
      public bool TriedToValidateKey
      {
          get { return m_TriedToValidateKey; }
          set { m_TriedToValidateKey = value; RaisePropertyChanged("TriedToValidateKey"); }
      }
  
      private string m_KeyValidationButtonText = "Verify Key";
      public string KeyValidationButtonText
      {
          get { return m_KeyValidationButtonText; }
          set { m_KeyValidationButtonText = value; RaisePropertyChanged("KeyValidationButtonText"); }
      }
  
      private Brush m_VerifyButtonBackground = new SolidColorBrush(Color.FromArgb(255, 213, 213, 213));
      public Brush VerifyButtonBackground
      {
          get { return m_VerifyButtonBackground; }
          set { m_VerifyButtonBackground = value; RaisePropertyChanged("VerifyButtonBackground"); }
      }
  
      public async void VerifyAPIKey()
      {
          //Task&lt;Paged&lt;Mailbox&gt;&gt; testPull = new Task&lt;Paged&lt;Mailbox&gt;&gt;(() =&gt; client.ListMailboxes());
          Task&lt;bool&gt; results = new Task&lt;bool&gt;(() =&gt; SettingsWindowLogic.VerifyAPIKey(APIKey));
          CurrentlyVerifyingKey = true;
  
          results.Start();
  
          CurrentlyVerifyingKey = false;
  
          if (await results)
          {
              ValidKey = true;
          }
          else
          {
              ValidKey = false;
          }
          TriedToValidateKey = true;
      }
  
  
  
      public void SaveSettings()
      {
          SettingsWindowLogic.SaveSettings(this);
          logger.Log(LogLevel.Debug, "Saved Settings");
          CloseFlyout();
      }
  
      public void LoadSettings()
      {
          SettingsWindowLogic.LoadSettings(this);
      }
      public void ResetSettings()
      {
          APIKey = string.Empty;
          SaveAPIKey = false;
          ApplicationData.ApplicationSettings.ValidAPIKeyExists = false;
          logger.Log(LogLevel.Debug, "Reset Settings");
      }
  
      public void CloseFlyout()
      {
          MainWindow.SettingsFlyout.IsOpen = false;
          logger.Log(LogLevel.Info, "Closed Settings Flyout");
      }
  }
  
  public class UserListViewModel : ViewModelBase
  {
      public UserListViewModel()
      {
         // LoadUserList();
      }
  
      public static Logger logger = LogManager.GetLogger("UserListViewModel");
  
      public MainWindow MainWindow { get; set; }
      public UserListView UserListView { get; set; }
  
      private UserList m_UserList;
      public UserList UsersList
      {
          get { return m_UserList; }
          set { m_UserList = value; RaisePropertyChanged("UsersList"); }
      }
  
      private List&lt;string&gt; m_TestItems;
      public List&lt;string&gt; TestItems
      {
          get { return m_TestItems; }
          set { m_TestItems = value; RaisePropertyChanged("TestItems"); }
      }
  
      private string m_NewUserName;
      public string NewUserName
      {
          get { return m_NewUserName; }
          set { m_NewUserName = value; RaisePropertyChanged("NewUserName"); }
      }
  
      private string m_HelpScoutUserListStatus = "Attempting To Load HelpScout Users...";
      public string HelpScoutUserListStatus
      {
          get { return m_HelpScoutUserListStatus; }
          set { m_HelpScoutUserListStatus = value; RaisePropertyChanged("HelpScoutUserListStatus"); }
      }
  
      private Brush m_HelpScoutUserListStatusColor = new SolidColorBrush(Color.FromArgb(255, 187, 95, 32));
      public Brush HelpScoutUserListStatusColor
      {
          get { return m_HelpScoutUserListStatusColor; }
          set { m_HelpScoutUserListStatusColor = value; RaisePropertyChanged("HelpScoutUserListStatusColor"); }
      }
  
      private bool m_HelpScoutUserListLoaded;
      public bool HelpScoutUserListLoaded
      {
          get { return m_HelpScoutUserListLoaded; }
          set 
          { 
              if(value)
              {
                  HelpScoutUserListStatus = UserListWindowLogic.HelpScutUseListStringStatus[0];
                  HelpScoutUserListStatusColor = UserListWindowLogic.HelpScoutUserListStatusColors[0];
                  ReverifyUserList();
              }
              else
              {
                  HelpScoutUserListStatus = UserListWindowLogic.HelpScutUseListStringStatus[1];
                  HelpScoutUserListStatusColor = UserListWindowLogic.HelpScoutUserListStatusColors[1];
              }
  
              m_HelpScoutUserListLoaded = value; 
              RaisePropertyChanged("HelpScoutUserListLoaded"); 
          }
      }
  
      private List&lt;User&gt; m_HelpScoutUsersList;
      public List&lt;User&gt; HelpScoutUsersList
      {
          get { return m_HelpScoutUsersList; }
          set { m_HelpScoutUsersList = value; RaisePropertyChanged("HelpScoutUsersList"); }
      }
  
      private List&lt;string&gt; m_HelpScoutUsersListStrings = new List&lt;string&gt;();
      public List&lt;string&gt; HelpScoutUsersListStrings
      {
          get { return m_HelpScoutUsersListStrings; }
          set { m_HelpScoutUsersListStrings = value; RaisePropertyChanged("HelpScoutUsersListStrings"); }
      }
  
      public async void RetrieveHelpScoutUserList()
      {
          Task&lt;List&lt;User&gt;&gt; task = new Task&lt;List&lt;User&gt;&gt;(() =&gt; UserListWindowLogic.RetrieveHelpScoutUserList());
          task.Start();
          List&lt;User&gt; usersList = await task;
          if(usersList != null)
          {
              HelpScoutUsersList = usersList;
              foreach(User userObject in usersList)
              {
                  HelpScoutUsersListStrings.Add(userObject.Name);
              }
              HelpScoutUserListLoaded = true;
          }
          else
          {
              HelpScoutUserListLoaded = false;
          }
      }
  
      private User MatchUserID(string name)
      {
          return UserListWindowLogic.FindUserByName(name, HelpScoutUsersList);
      }
  
      public void RemoveUser()
      {
          User user = UserListView.NamesDataGrid.SelectedItem as User;
          UsersList.Users.Remove(user);
          logger.Log(LogLevel.Debug, "Removed User: " + user.Name);
      }
  
      public void AddUser()
      {
          if (!string.IsNullOrEmpty(NewUserName) &amp;&amp; HelpScoutUserListLoaded)
          {
              User user = MatchUserID(NewUserName);
              if(user != null)
              {
                  UsersList.Users.Add(user);
                  logger.Log(LogLevel.Debug, "Added New Valid User: " + user.Name);
              }
              else
              {
                  UsersList.Users.Add(new User() { Name = NewUserName });
                  logger.Log(LogLevel.Debug, "Added New User: " + NewUserName);
              }
          }
          else
          {
              UsersList.Users.Add(new User() { Name = NewUserName });
              logger.Log(LogLevel.Debug, "Added New User: " + NewUserName);
          }
          ClearNewUserNameTextBox();
      }
  
      //Clears the new user textbox
      public void ClearNewUserNameTextBox()
      {
          NewUserName = string.Empty;
      }
  
      public void SetupFreshViewModel()
      {
          m_HelpScoutUserListLoaded = false;
          HelpScoutUserListStatusColor = new SolidColorBrush(Color.FromArgb(255, 187, 95, 32));
          HelpScoutUserListStatus = "Attempting To Load HelpScout Users...";
          LoadUserList();
          RetrieveHelpScoutUserList();
      }
  
      public void SaveUserList()
      {
          UserListWindowLogic.SaveUserList(this);
          logger.Log(LogLevel.Debug, "Saved Users List");
          CloseFlyout();
      }
  
      public void ReverifyUserList()
      {
          UserListWindowLogic.CheckUserListValidity(HelpScoutUsersList);
          LoadUserList();
      }
  
      public void LoadUserList()
      {
          UserList userList;
          if(ApplicationData.Users != null)
          {
              userList = XMLSerialize&lt;UserList&gt;.CopyData(ApplicationData.Users);
          }
          else
          {
              userList = UserListWindowLogic.CreateNewUserList();
          }
          UsersList = userList;
      }
  
      public void ResetUserList()
      {
          UsersList = new UserList();
          logger.Log(LogLevel.Debug, "Reset Users List");
      }
  
      public void CloseFlyout()
      {
          MainWindow.UserListFlyout.IsOpen = false;
          logger.Log(LogLevel.Info, "Closed Users List Flyout");
      }
  }
  
  public class ViewModelBase : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      public void RaisePropertyChanged(String propertyName)
      {
          PropertyChangedEventHandler temp = PropertyChanged;
          if (temp != null)
          {
              temp(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
      public bool IsInDesignMode
      {
          get
          {
              var prop = DesignerProperties.IsInDesignModeProperty;
              return (bool)DependencyPropertyDescriptor
                  .FromProperty(prop, typeof(FrameworkElement))
                  .Metadata.DefaultValue;
          }
      }
  }
  </code></pre>