Xamarin Sqlite-Net Insert(对象引用未设置为对象的实例)

时间:2016-08-09 16:20:22

标签: c# xamarin xamarin.android sqlite-net sqlite-net-extensions

我有一个非常奇怪的问题,我无法弄明白。我无法在我的Sqlite数据库中插入新项目。我一直收到这个错误: -

System.NullReferenceException: Object reference not set to an instance of an object.

我完全不知道为什么并且使用完全相同的代码并在应用程序中保存其他表。

这是我的代码: -

User.cs

public class User
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string Name { get; set; }
    public string EmailAddress { get; set; }

    public User()
    {
        this.Name = " ";
        this.EmailAddress = " ";
    }
}

UserManager.cs

public class UserManager 
{
    UserRepository repository; 

    public UserManager(SQLiteConnection conn)
    {
        repository = new UserRepository (conn);
    }

    public User GetDriver(int id)
    {
        return repository.GetDriver(id);
    }

    public IList<User> GetDrivers()
    {
        return new List<User>(repository.GetDrivers());
    }

    public int SaveDriver(User item)
    {
        return repository.SaveDriver(item);
    }

    public int DeleteDriver(int id)
    {
        return repository.DeleteDriver(id);
    }
}

UserRepository.cs

public class UserRepository
{
    UserDatabase db = null;

    public UserRepository(SQLiteConnection conn)
    {
        db = new UserDatabase(conn);
    }

    public User GetDriver(int id)
    {
        return db.GetDriverItem(id);
    }

    public IEnumerable<User> GetDrivers()
    {
        return db.GetAllDriverItems();
    }

    public int SaveDriver(User item)
    {
        return db.SaveDriverItem(item);
    }

    public int DeleteDriver(int id)
    {
        return db.DeleteDriverItem(id);
    }
}

UserDatabase.cs

public class UserDatabase
{
    static object locker = new object();
    public SQLiteConnection database;
    public string path;

    public UserDatabase(SQLiteConnection conn)
    {
        database = conn;
        database.CreateTable<User>();
    }

    public IEnumerable<User> GetAllDriverItems () {
        lock (locker) {
            return (from i in database.Table<User>() select i).ToList();
        }
    }

    public User GetDriverItem (int id) 
    {
        lock (locker) {
            return database.Table<User>().FirstOrDefault(x => x.ID == id);
        }
    }

    public int SaveDriverItem (User item) 
    {
        lock (locker) {
            if (item.ID != 0) {
                database.Update(item);
                return item.ID;
            } else {
                return database.Insert(item);
            }
        }
    }

    public int DeleteDriverItem(int id) 
    {
        lock (locker) {
            return database.Delete<User>(id);
        }
    }
}

Application.cs(在Android中)

public class UserApp : Application
{
    public static UserApp Current { get; private set; }

    public UserManager UserManager { get; set; }

    SQLiteConnection conn;

    public UserApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
        : base(handle, transfer)
    {
        Current = this;
    }

    public override void OnCreate()
    {
        base.OnCreate();

        var sqliteFilename = "UserItemDB.db3";
        string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var path = Path.Combine(libraryPath, sqliteFilename);
        conn = new SQLiteConnection(new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(), path);

        UserManager = new UserManager(conn);

        User testUser= new User();
        testUser.Name = "Dave";
        testUser.EmailAddress = "Dave@Gmail.com";
        UserApp.Current.UserManager.SaveDriver(testUser);
    }

这是我的堆栈跟踪: -

System.NullReferenceException: Object reference not set to an instance of an object.
08-09 17:07:32.423 E/mono    (31670):   at     System.Reflection.MonoProperty.GetValue (System.Object obj, System.Object[]     index) [0x00031] in <filename unknown>:0 
08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.TableMapping+Column.GetValue (System.Object obj) [0x00000] in     <filename unknown>:0 
08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.SQLiteConnection.Insert (System.Object obj, System.String extra,     System.Type objType) [0x00098] in <filename unknown>:0 
08-09 17:07:32.423 E/mono    (31670):   at     SQLite.Net.SQLiteConnection.Insert (System.Object obj) [0x00012] in <filename     unknown>:0 
08-09 17:07:32.423 E/mono    (31670):   at     Project.DataLayer.UserDatabase.SaveDriverItem (Project.BusinessLayer.Models.User     item) [0x00038] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\DataLayer\UserDat    abase.cs:42 
08-09 17:07:32.423 E/mono    (31670):   at     Project.DataAccessLayer.UserRepository.SaveDriver     (Project.BusinessLayer.Models.User item) [0x00001] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\DataAccessLayer\U    serRepository.cs:30 
08-09 17:07:32.423 E/mono    (31670):   at     Project.BusinessLayer.Managers.UserManager.SaveDriver     (Project.BusinessLayer.Models.User item) [0x00001] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Shared\BusinessLayer\Man    agers\UserManager.cs:33 
08-09 17:07:32.423 E/mono    (31670):   at Project.Droid.UserApp+    <getDriverDetails>d__16.MoveNext () [0x000e2] in     C:\Users\phill\Desktop\Work\ProjectTEST\Project\Project.Droid\Application.cs:75 

1 个答案:

答案 0 :(得分:1)

解决了 - 经过长时间的头痛我终于找到了问题。

好的,所以NullObject引用数据库文件本身。由于Xamarin中的某些设置,无法更改数据库的结构,并且我的应用程序无法创建新的数据库。不是100%肯定这些是确切的答案,但我的项目最终在...之后不久工作。

在VS中转到工具&gt;选项&gt; Xamarin&gt; Android&gt;勾选&#34;保留应用数据&#34;

我认为这是主要问题^^。我也检查过/取消选中其他人。

项目&gt; Project.Properties&gt; Android选项&gt; Tick Shared Runtime&amp;快速部署和下一个选项卡链接器=无。

不确定上述哪一项解决了我的问题,但它运行得很好