【问题标题】:Why Do I Get Null Reference Exception When Trying to Get a SQLite Database Connection?为什么尝试获取 SQLite 数据库连接时出现空引用异常?
【发布时间】:2019-02-04 14:35:36
【问题描述】:

我正在尝试创建一个 SQLite 数据库,但出现异常。该错误没有说明异常发生的原因。它只是说一个空引用异常。 异常发生在这一行

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

在构造函数中。

这是我的 .cs 文件

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)
        {

        }
    }
}

这是我用来通过在 iOS 和 Android 中以不同方式实现它来获取数据库路径的路径

>     
    > using SQLite;
    > using System;
    > using System.Collections.Generic;
    > using System.Text;
    > 
    > namespace App4.Persistence
    > {
    >     public interface ISQLiteDB
    >     {
    >         SQLiteAsyncConnection GetConnection();
    > 
    >     }
    > }

这是 iOS 和 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);

        }

    }
}

虽然 iOS 和 Android 的实现是相同的(并且在这种情况下不需要接口),但我将为需要不同实现以获取数据库路径的窗口创建项目。

感谢任何帮助。

谢谢。

【问题讨论】:

    标签: sqlite xamarin.forms nullreferenceexception


    【解决方案1】:

    异常的问题是没有在实现类中定义依赖属性,我应该在命名空间定义上方而不是在类中使用[assembly: Dependency (typeof (SQLiteDB)]

    虽然我在尝试通过依赖属性注册实现类时仍然遇到另一个错误。

    我收到此错误:

    没有与形参对应的实参 "LoadHintArgument" 或 DependecyAtrrbute.DependencyAttribute(string 加载提示)。

    感谢有关该主题的任何帮助。

    【讨论】:

      【解决方案2】:

      在我的情况下,这个错误只存在于 Xamarin IOS 开发中,而不存在于 Android 中。已修复在“电池”初始化数据库中添加奇怪的行:

              SQLitePCL.Batteries_V2.Init(); // <-- LINE TO ADD
              string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
              string libFolder = System.IO.Path.Combine(docFolder, "..", "Library", "Databases");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-19
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多