title: Xamarin Sqlite-Net Insert (Object reference not set to an instance of an object)
body: <p>I have a very strange problem that I can't figure out. I cannot insert a new item into my Sqlite DB. I keep receiving this error:-</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>I literally have no idea why and have the exact same code working and saving other tables within the application.</p>
  
  <p>Here is my code:-</p>
  
  <p><strong>User.cs</strong></p>
  
  <pre><code>public class User
  {
      [PrimaryKey, AutoIncrement]
      public int ID { get; set; }
      public string Name { get; set; }
      public string EmailAddress { get; set; }
  
      public User()
      {
          this.Name = " ";
          this.EmailAddress = " ";
      }
  }
  </code></pre>
  
  <p><strong>UserManager.cs</strong></p>
  
  <pre><code>public class UserManager 
  {
      UserRepository repository; 
  
      public UserManager(SQLiteConnection conn)
      {
          repository = new UserRepository (conn);
      }
  
      public User GetDriver(int id)
      {
          return repository.GetDriver(id);
      }
  
      public IList&lt;User&gt; GetDrivers()
      {
          return new List&lt;User&gt;(repository.GetDrivers());
      }
  
      public int SaveDriver(User item)
      {
          return repository.SaveDriver(item);
      }
  
      public int DeleteDriver(int id)
      {
          return repository.DeleteDriver(id);
      }
  }
  </code></pre>
  
  <p><strong>UserRepository.cs</strong></p>
  
  <pre><code>public class UserRepository
  {
      UserDatabase db = null;
  
      public UserRepository(SQLiteConnection conn)
      {
          db = new UserDatabase(conn);
      }
  
      public User GetDriver(int id)
      {
          return db.GetDriverItem(id);
      }
  
      public IEnumerable&lt;User&gt; GetDrivers()
      {
          return db.GetAllDriverItems();
      }
  
      public int SaveDriver(User item)
      {
          return db.SaveDriverItem(item);
      }
  
      public int DeleteDriver(int id)
      {
          return db.DeleteDriverItem(id);
      }
  }
  </code></pre>
  
  <p><strong>UserDatabase.cs</strong></p>
  
  <pre><code>public class UserDatabase
  {
      static object locker = new object();
      public SQLiteConnection database;
      public string path;
  
      public UserDatabase(SQLiteConnection conn)
      {
          database = conn;
          database.CreateTable&lt;User&gt;();
      }
  
      public IEnumerable&lt;User&gt; GetAllDriverItems () {
          lock (locker) {
              return (from i in database.Table&lt;User&gt;() select i).ToList();
          }
      }
  
      public User GetDriverItem (int id) 
      {
          lock (locker) {
              return database.Table&lt;User&gt;().FirstOrDefault(x =&gt; x.ID == id);
          }
      }
  
      public int SaveDriverItem (User item) 
      {
          lock (locker) {
              if (item.ID != 0) {
                  database.Update(item);
                  return item.ID;
              } else {
                  return database.Insert(item);
              }
          }
      }
  
      public int DeleteDriverItem(int id) 
      {
          lock (locker) {
              return database.Delete&lt;User&gt;(id);
          }
      }
  }
  </code></pre>
  
  <p><strong>Application.cs (In Android)</strong></p>
  
  <pre><code>public class UserApp : Application
  {
      public static UserApp Current { get; private set; }
  
      public UserManager UserManager { get; set; }
  
      SQLiteConnection conn;
  
      public UserApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
          : base(handle, transfer)
      {
          Current = this;
      }
  
      public override void OnCreate()
      {
          base.OnCreate();
  
          var sqliteFilename = "UserItemDB.db3";
          string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
          var path = Path.Combine(libraryPath, sqliteFilename);
          conn = new SQLiteConnection(new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(), path);
  
          UserManager = new UserManager(conn);
  
          User testUser= new User();
          testUser.Name = "Dave";
          testUser.EmailAddress = "Dave@Gmail.com";
          UserApp.Current.UserManager.SaveDriver(testUser);
      }
  </code></pre>
  
  <p>Here is my stack trace:-</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
  08-09 17:07:32.423 E/mono    (31670):   at     System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[]     index) [0x00031] in &lt;filename unknown&gt;:0 
  08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.TableMapping+Column.GetValue (System.Object obj) [0x00000] in     &lt;filename unknown&gt;:0 
  08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.SQLiteConnection.Insert (System.Object obj, System.String extra,     System.Type objType) [0x00098] in &lt;filename unknown&gt;:0 
  08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.SQLiteConnection.Insert (System.Object obj) [0x00012] in &lt;filename     unknown&gt;:0 
  08-09 17:07:32.423 E/mono    (31670):   at     Project.DataLayer.UserDatabase.SaveDriverItem (Project.BusinessLayer.Models.User     item) [0x00038] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\DataLayer\UserDat    abase.cs:42 
  08-09 17:07:32.423 E/mono    (31670):   at     Project.DataAccessLayer.UserRepository.SaveDriver     (Project.BusinessLayer.Models.User item) [0x00001] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\DataAccessLayer\U    serRepository.cs:30 
  08-09 17:07:32.423 E/mono    (31670):   at     Project.BusinessLayer.Managers.UserManager.SaveDriver     (Project.BusinessLayer.Models.User item) [0x00001] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\BusinessLayer\Man    agers\UserManager.cs:33 
  08-09 17:07:32.423 E/mono    (31670):   at Project.Droid.UserApp+    &lt;getDriverDetails&gt;d__16.MoveNext () [0x000e2] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Droid\Application.cs:75 
  </code></pre>