title: How to use local database in Xamarin.form?
body: <p>I'm following a tutorial step by step to create a local database by using SQLite.net-PCL. However, when running the app, I tried to insert a data into the database, but it threw me a 'System.NullReferenceException'.</p>
  
  <p>What I have done:</p>
  
  <ul>
  <li>I'm only using SQLite.Net-TCL (1.2.1) package, and also added it into Android and iOS.</li>
  <li>I noticed some tutorials saying that I should implement sqlite database on android and iOS, but I didn't find the tutorial is using this way, or maybe the IFileHelper interface has the same function?</li>
  <li>I keep FileHelper interface in my Android folder</li>
  <li>After debug, the database code passed. So the problem should happen when inserting data.</li>
  <li>The exception happened at this line: if (person.ID != 0), and this line : await App.DB.SaveItemAsync(person);</li>
  </ul>
  
  <p>Tutorial: <a href="https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/" rel="nofollow noreferrer">https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/</a></p>
  
  <p>This is my database code:</p>
  
  <pre><code>using System.Threading.Tasks;
  using System.Collections.Generic;
  using System;
  using SQLite;
  
  namespace DatabaseDemo
  {
  public class Database
  {
      readonly SQLiteAsyncConnection _database;
  
      public Database(string path)
      {
          _database = new SQLiteAsyncConnection(path);
          _database.CreateTableAsync&lt;Model&gt;().Wait();
      }
  
      public Task&lt;List&lt;Model&gt;&gt; GetItemsAsync()
      {
          return _database.Table&lt;Model&gt;().ToListAsync();
      }
  
      public Task&lt;int&gt; SaveItemAsync(Model person)
      {
          if (person.ID != 0)
          {
              return _database.UpdateAsync(person);
  
          }
          else
          {
              return _database.InsertAsync(person);
          }
      }
  }
  }
  </code></pre>
  
  <p>This is my interface:</p>
  
  <p>
  </p>
  
  <pre><code>&lt;StackLayout Margin="20" VerticalOptions="StartAndExpand"&gt;
      &lt;Label Text = "Name"/&gt;
      &lt;Entry Text = "{Binding Name}" /&gt;
      &lt;Button Text = "Save" Clicked="OnSaveClicked" /&gt;
  &lt;/StackLayout&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>using Xamarin.Forms;
  using System;
  
  namespace DatabaseDemo
  {
      public partial class DatabaseDemoPage : ContentPage
      {
          public DatabaseDemoPage()
          {
              InitializeComponent();
          }
  
      async void OnSaveClicked(object sender, EventArgs e)
      {
          var person = (Model)BindingContext;
          await App.DB.SaveItemAsync(person);
      }
  }
  }
  </code></pre>
  
  <p>App.cs</p>
  
  <pre><code>using Xamarin.Forms;
  
  namespace DatabaseDemo
  {
      public partial class App : Application
      {
          static Database database;
          public static Database DB
          {
              get
              {
                  if (database == null)
                  {
                      database = new Database(DependencyService.Get&lt;IFileHelper&gt;().GetLocalFilePath("db.db3"));
                  }
                  return database;
              }
          }
  
          public App()
          {
              InitializeComponent();
  
              MainPage = new DatabaseDemoPage();
          }
      }
  }
  </code></pre>
  
  <p>I also created a public interface, and also created for Andriod and iOS.</p>
  
  <pre><code>using System;
  namespace DatabaseDemo
  {
      public interface IFileHelper
      {
          string GetLocalFilePath(string filename);
      }
  }
  </code></pre>