Xamarin Sqlite-Net插入(对象引用未设置为对象的实例)
本文关键字:对象 实例 设置 Sqlite-Net 插入 对象引用 Xamarin | 更新日期: 2023-09-27 18:01:36
我有一个很奇怪的问题,我不明白。我不能插入一个新的项目到我的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
已解决-经过长时间的头痛,我终于找到了问题所在。
NullObject指向数据库文件本身。由于Xamarin中的一些设置,数据库的结构无法更改,我的应用程序无法创建新的数据库。我不能百分之百确定哪一个是确切的答案,但我的项目最终工作了不久之后…
在VS中进入工具>选项> Xamarin> Android>勾选"保存应用程序数据"
我认为这是主要问题^^。我也检查过/检查过其他人。
Project>Project。属性> Android选项>勾选共享运行时&;
不确定上面哪个解决了我的问题,但它运行得很好