0
votes

Xamarin forms newbie here, and I am trying to establish an SQLite connection so that I may interact with a created database in my application.

I get no flags on this in the IDE, but when I compile, I'm getting a null reference error! Specifically, my _SQLiteConnection var is null, and I think it may have something to do with not setting the path properly?

I've referenced a few guides on doing this online, and although a little dated, they go about setting this up in the same manner I am.

Heres some code:

UserDb.cs:

using PluralBuddy.Models;
using SQLite;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PluralBuddy.Data
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public class UserDB
    {
        private SQLiteConnection _SQLiteConnection;

        public UserDB()
        {

            _SQLiteConnection = DependencyService.Get<ISQLiteInterface>().GetConnection();
            _SQLiteConnection.CreateTable<User>();
        }

ISQLiteInterface.cs:

using SQLite;

namespace PluralBuddy.Models
{
    public interface ISQLiteInterface
    {
        SQLiteConnection GetConnection();
    }
}

Nullreference

_SQLiterConnection is null

References:

https://code.tutsplus.com/tutorials/an-introduction-to-xamarinforms-and-sqlite--cms-23020

https://dzone.com/articles/register-and-login-using-sqlite-in-xamarinforms

As always, any help is appreciated!

1
The issue should be here DependencyService.Get<ISQLiteInterface>() check that this does not return null. If it is check how you handle the registration. - Guru Stron
The error line is on the call to DependencyService. Did you complete the tutorial code before trying to run it? - Steve
yes I did to my knowledge. I can go back and double-check however. - Derrick Wright
SQLite.NET no longer requires using DependencyService. Read the latest docs for examples. If you want to continue to use your current method, break the Get() and GetConnection() into 2 different lines so you can tell which one is causing the null ref. - Jason
Please open your dependenceService code, please make sure set the attribute if it is correctly, For exmple, I define a interface is ISimService, and achieve this ISimServiceinterface by SimNumber class, I need set the attribute is [assembly: Dependency(typeof(SimNumber))] like this sceenshot. imgur.com/a/sfIhDDS - Leon Lu - MSFT

1 Answers

0
votes

So for anyone who stumbles across this post looking for an answer to a similar problem, make sure that you are initializing your database and giving it a path. Be very careful following guides as they will often leave out vital context, either on purpose or by mistake.

Here is what I did

in App.Xaml

using PluralBuddy.Data;
using PluralBuddy.Views;
using System;
using System.IO;
using Xamarin.Forms;

namespace PluralBuddy
{
    public partial class App : Application
    {

        static UserDB userDB;

        public static UserDB UserDB
        {
            get
            {
                if (userDB == null)
                {
                    userDB = new UserDB(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "user.db3"));
                }
                return userDB;
            }
        }

UserDB.cs

namespace PluralBuddy.Data
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public class UserDB
    {
        private readonly SQLiteConnection _SQLiteConnection;

        public UserDB(string dbPath)
        {
            _SQLiteConnection = new SQLiteConnection(dbPath);
            _SQLiteConnection.CreateTable<User>();
        }


and just in case anyone needs it, here is a more up to date guide from one of the same guys as referenced in the original post:

https://xmonkeys360.com/2019/07/10/register-login-and-update-using-sqlite-in-xamarin-forms/