Why Do I Get Null Reference Exception When Trying to Get a SQLite Database Connection ?

DyaryDyary Member ✭✭

Hi. I am trying to create a SQLite Dabase but I get an Exception. The error doesn't tell why the exception occurs. It just says a Null Reference Exception.
The Exception Occurs at this line

var connection = DependencyService.Get<ISQLiteDB>().GetConnection();

in the constructor.

here is my .cs file

using App4.Persistence;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App4
{

    public class Contact
    {

        [PrimaryKey,AutoIncrement]
        public int  Id { get; set; }

        [MaxLength (255)]
        public string Name { get; set; }
    }

    public partial class MainPage : ContentPage
    {

        public string ConnectionPath { get; set; }
        public MainPage()
        {
            InitializeComponent();

            var connection = DependencyService.Get<ISQLiteDB>().GetConnection();


        }

        protected async override void OnAppearing()
        {
            var connection = new SQLiteAsyncConnection(ConnectionPath);

            await connection.CreateTableAsync<Contact>();
            var contacts = await connection.Table<Contact>().ToListAsync();
            listViewContacts.ItemsSource = contacts;


            base.OnAppearing();
        }

        private void buttonAdd_Clicked(object sender, EventArgs e)
        {

        }

        private void buttonDelete_Clicked(object sender, EventArgs e)
        {

        }

        private void buttonUpdate_Clicked(object sender, EventArgs e)
        {

        }
    }
}

Here is the interfaith I use for getting the path to the database path by implementing it differently in iOS and Android


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

namespace App4.Persistence
{
public interface ISQLiteDB
{
SQLiteAsyncConnection GetConnection();

}

}

and this is the implementation in both iOS and Android.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App4.Persistence;
using SQLite;

namespace App4.Droid.Persistence
{
   public class SQLiteDB : ISQLiteDB
    {
        public SQLiteAsyncConnection GetConnection()
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            var  DatabasePath = Path.Combine(documentsPath,"MySQLite.db3");

            return new SQLiteAsyncConnection(DatabasePath);

        }

    }
}

although the implementation for iOS and Android are the same ( And the interface in this case isn't needed), I will be creating projects for windows requiring different implementation for getting the path to the database.

Any help is appreciated guys.

Thanks.

Best Answer

Answers

  • DyaryDyary Member ✭✭
    edited August 2018

    Thank you so much for you kind answer.
    I think that is what was causing the error as i have forgotten to register the implementation classes , although i still get this message when I apply the dependency attribute.

    There is no argument that corresponds to the formal parameter "LoadHintArgument" or DependecyAtrrbute.DependencyAttribute(string LoadHint).

  • DyaryDyary Member ✭✭

    By adding "using Xamarin.Forms" the squiggly line under the attribute definition disappear in android. but still have an issue with the iOs saying " the namespace or type SQLiteDB could not be found, are you missing an assembly reference or cast?)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Ya probably have other missing using statements or similar.
    ReSharper is great about poping up Intellisense suggesting what to add. It does a good job of finding and suggesting missing namespaces and so on.

  • DyaryDyary Member ✭✭

    Thank you so much.

Sign In or Register to comment.