0

Recently I started to learn Xamarin Forms to make cros platform apps instead of just native ones, right now im trying to make use of SQLite to save data, it works in the UWP proyect but it doesn't in the Android proyect, (Haven't tested in IOS), I use a class to administrated the data in the main General proyect, a particular class to create the database in each platform, and and interface to connect the main class which those, at the moment of saving data in the the Android proyect I got an exception, although it's the same code I use with the UWP(Besides the particular DB generator)

INTERFACE

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace Navigation
{
    public interface ISQLite
    {
        SQLite.Net.SQLiteConnection GetConnection();
    }
}

--DataBase class with the methods--

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;

namespace Navigation.Class
{
    class DBHelper
    {
        private SQLite.Net.SQLiteConnection _connection;

        public DBHelper()
        {
            _connection = DependencyService.Get<ISQLite>().GetConnection();
            _connection.CreateTable<Model.Agent>();
        }

        public void rebirth() {
            _connection.DropTable<Model.Agent>();
            _connection.CreateTable<Model.Agent>();
        }

        public void killAgents()
        {
            _connection.DeleteAll<Model.Agent>();

        }

        public List<Model.Agent> GetAgents()
        {
            return _connection.Table<Model.Agent>().ToList();
        }

        public Model.Agent GetAgent(int id)
        {
            return _connection.Table<Model.Agent>()
                              .FirstOrDefault(t => t.ID == id);
        }

        public void DeleteAgent(int id)
        {
            _connection.Delete<Model.Agent>(id);
        }

        public int AddAgent(Model.Agent agent)
        {
          return  _connection.Insert(agent);
        }
    }
}

The SQLite Class of the UWP project

using System.IO;
using Windows.Storage;
using Xamarin.Forms;
using Navigation.UWP;

[assembly: Dependency(typeof(SQLite_UWP))]
namespace Navigation.UWP
{
    class SQLite_UWP:ISQLite
    {
        public SQLite_UWP() { }

        public SQLite.Net.SQLiteConnection GetConnection()
        {
            var fileName = "databaseTest.db3";
            var path = Path.Combine(ApplicationData.Current.LocalFolder.Path, fileName);

            var platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT();
            var connection = new SQLite.Net.SQLiteConnection(platform, path);

            return connection;
        }
    }
}

SQLite class of the android project

using System;
using System.IO;
using Navigation.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_Android))]
namespace Navigation.Droid
{
    class SQLite_Android:ISQLite
    {
        public SQLite_Android() { }

        public SQLite.Net.SQLiteConnection GetConnection() 
        {
            var fileName = "databaseTest.db3";
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            var path = Path.Combine(documentsPath, fileName);
            var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
            var connection = new SQLite.Net.SQLiteConnection(platform, path);

            return connection;
        }
    }
}

Shared fragment of code which works with UWP but gives the Exception in Android

try 
{
    int result = dBHelper.AddAgent(x);
    await DisplayAlert("RESULT", "TRANSACTION RESULT" + result, "OKA");
}
catch (Exception eMessage) 
{
    System.Diagnostics.Debug.Print("Exception MESSAGE", "" + eMessage);
    await DisplayAlert("CRASH", "EXCEPTION RESULT" + eMessage, "OKA");
}

Exception

RESULTSystem.NullReferenceException: Object reference not set to an             
instance of an object at 
System.Reflection.MonoProperty.GetValue (System.Object.obj, 
System.Object[] index)[0x00027] in
<f32579baafc1404fa37ba3ec1abdc0bd>:0
at SQLite.Net.TableMapping
+Column.GetValue (System.Object
obj) [0x00000] in 
<f32579baafc1404fa37ba3ec1abdc0bd>:0
at
SQLite.Net.SQLiteConnection.Insert
(System.Object obj, System.String
extra, System.Type objType)
[0x00098] in 
<f32579baafc1404fa37ba3ec1abdc0bd>:0
at

SQLite.Net.SQLiteConnection.Insert
(System.Object obj)[0x00012] in 
<f32579baafc1404fa37ba3ec1abdc0bd>:0
at
Navigation.Class.DBHelper.AddAgent(Navigation.Model.Agent agent)
[0x00000] in 
<c5c816c1cd7e40428bca910521979232>:0
at Navigation.Admin+<ActionButton_Clicked>d_4.MoveNext()[0x00483] in 
<c5c816c1cd7e40428bca910521979232>:0
Presto
  • 806
  • 9
  • 24
Andrew
  • 53
  • 5

0 Answers0