title: Error: System.NullReferenceException: Object reference not set to an instance of an object
body: <p>Work with Windows-phone app C#:</p>
  
  <p>I get this error when add my info <strong>"System.NullReferenceException: Object reference not set to an instance of an object."</strong> tại "<strong>Data.Add(newInfo)</strong>" I use MVVM pattern.</p>
  
  <p>In <strong>MainViewModel.cs</strong>, my <strong>Add Function</strong> is</p>
  
  <pre><code>public class MainViewModel : INotifyPropertyChanged
  {
      private MemberDataContext MemberDB;
  
      public MainViewModel(string MemberDBConnectionString)
      {
          MemberDB = new MemberDataContext(MemberDBConnectionString);
      }
  
  
      private ObservableCollection&lt;Member&gt; _data;
      public ObservableCollection&lt;Member&gt; Data
      {
          get { return _data; }
          set
          {
              _data = value;
              NotifyPropertyChanged("Data");
          }
      }
  
      public void LoadCollectionsFromDatabase()
      {
          var infoInDB = from Member ss in MemberDB.Members
                         select ss;
          Data = new ObservableCollection&lt;Member&gt;(infoInDB);
      }
  
  
      public void Addinfo(Member newInfo)
      {
          MemberDB.Members.InsertOnSubmit(newInfo);
  
          MemberDB.SubmitChanges();
          Data.Add(newInfo); **&lt;~~~~~~~ Error in this line**
  
      }
  
      public void Update(Member currentmember, string fullname, string address)
      {
          currentmember.Address = address;
          currentmember.FullName = fullname;
      }
  
      public void Delete(Member currentmember)
      {
          MemberDB.Members.DeleteOnSubmit(currentmember);
          Data.Remove(currentmember);
      }
  
      public void SaveData()
      {
          MemberDB.SubmitChanges();
      }
  
      public void LoadData()
      {
          if (Data == null)
          {
              Data = new ObservableCollection&lt;Member&gt;(MemberDB.Members);
              var oderedFullName = from Member b in MemberDB.Members
                                   orderby b.FullName
                                   select b;
          }
      }
  
      public MainViewModel()
      {
          MemberDB = new MemberDataContext("Data source=isostore:/Member.sdf");
          if (!MemberDB.DatabaseExists())
          {
              MemberDB.CreateDatabase();
          }
      }
  
  
  
      #region INotifyPropertyChanged Members
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      // Used to notify the app that a property has changed.
      private void NotifyPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
      #endregion
  
  }
  public class ViewDetails : INotifyPropertyChanged
  {
      private string _fullname;
  
      public string FullName
      {
          get
          {
              return _fullname;
          }
          set
          {
              _fullname = value;
  
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs("Fullname"));
              }
          }
      }
  
      private string _address;
  
      public string Address
      {
          get
          {
              return _address;
          }
          set
          {
              _address = value;
  
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs("Address"));
              }
          }
      }
      public void LoadDetails(Member abc)
      {
          FullName = abc.FullName;
          Address = abc.Address;
      }
      public void UpdateDetails(Member abc)
      {
          abc.FullName = FullName;
          abc.Address = Address;
      }
       #region INotifyPropertyChanged Members
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      // Used to notify the app that a property has changed.
      private void NotifyPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
      #endregion
  </code></pre>
  
  <p>My <strong>Add.xaml:</strong></p>
  
  <pre><code>&lt;StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;
  
          &lt;TextBlock Text="FullName"/&gt;
          &lt;TextBox x:Name="addfullname"/&gt;
          &lt;TextBlock Text="Address"/&gt;
          &lt;TextBox x:Name="addaddress" /&gt;
  
  
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>My <strong>Add Button:</strong></p>
  
  <pre><code> private void Add_Click(object sender, EventArgs e)
      {
          if ((addaddress.Text.Length &gt; 0) &amp;&amp; (addfullname.Text.Length &gt; 0))
          {
              Member newInfo = new Member
              {
                  FullName = addfullname.Text,
                  Address = addaddress.Text
              };
              App.MainViewModel.Add(newInfo);
  
              if (NavigationService.CanGoBack)
              {
                  NavigationService.GoBack();
              }
          }
          //else MessageBox.Show("Data cannot be null!");
  
      }
  </code></pre>
  
  <p>In my <strong>App.xaml</strong></p>
  
  <pre><code>  public partial class App : Application
  {
      private static object _lockObject = new object();
      private static MainViewModel mainviewmodel;
      public static MainViewModel MainViewModel
      {
          get
          {
  
  
              if (mainviewmodel == null)
              {
                lock (_lockObject)
                {
                    mainviewmodel = new MainViewModel();
                  }
              }
              return mainviewmodel;
          }
  
  
      }
  </code></pre>