【发布时间】:2017-05-03 04:52:50
【问题描述】:
我正在尝试使用 SQLiteConnection 从数据库中选择数据。这是一个 UWP 应用程序。
public class ResumeModel
{
public List<User> Users { get; set; } = new List<User>();
public ResumeModel()
{
using (var connection = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), App.path))
{
try
{
object query = connection.Query<User>("Select * From User", null);
if(query != null)
{
Users = (List <User>) query;
}
} catch(Exception ex)
{
Debug.Write(ex.ToString());
}
}
}
}
我遇到了异常:“对象引用未设置为对象的实例”
这是我的用户类:
public class User
{
[SQLite.Net.Attributes.PrimaryKey, SQLite.Net.Attributes.AutoIncrement]
public int userID { get; set; }
public String username { get; set; }
public User()
{ }
public User(int userID, string name)
{
this.userID = userID;
this.username = name;
}
}
有谁知道我做错了什么?
谢谢
【问题讨论】:
-
用谷歌搜索以下
C# Initialize Class with a List<T> property -
我认为问题不在于列表初始化,因为我在设置列表用户之前得到了异常