- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 NPoco 的 Delete() 方法从数据库中删除一行。然而它只是抛出一个 NullReferenceException。
我找到了解决方法,但我想知道是否有人知道为什么内置的按 ID 删除的删除功能似乎对我不起作用。这发生在多个表上。我的所有表都有一个称为 ID 的标准整数主键,它已在模型中使用 [PrimaryKey("ID")]
装饰器标记。
对象引用未设置到对象的实例。
Delete<PurchaseItem>(id); // throws null reference exception.
Delete<PurchaseItem>("where id = @0", id); // works.
传递的 id 有效且项目在数据库中。代码没有执行任何 SQL。
堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
NPoco.PocoDataFactory.ForObject(Object o, String primaryKeyName) in d:\Adam\projects\NPoco\src\NPoco\PocoDataFactory.cs:41
NPoco.Database.Delete(String tableName, String primaryKeyName, Object poco, Object primaryKeyValue) in d:\Adam\projects\NPoco\src\NPoco\Database.cs:1587
NPoco.Database.Delete(Object pocoOrPrimaryKey) in d:\Adam\projects\NPoco\src\NPoco\Database.cs:1598
Harmsworth.DAL.HarmsworthDB.DeletePurchaseItemFromBasketByID(Int32 id) in c:\inetpub\wwwroot\harmsworth\Website\classes\HarmsworthDAL.cs:224
Harmsworth.ViewBasketPage.RemoveItem(Int32 id) in c:\inetpub\wwwroot\harmsworth\Website\view-basket.aspx.cs:187
Harmsworth.ViewBasketPage.PurchaseItemsRepeater_ItemCommand(Object sender, RepeaterCommandEventArgs e) in c:\inetpub\wwwroot\harmsworth\Website\view-basket.aspx.cs:75
System.Web.UI.WebControls.Repeater.OnItemCommand(RepeaterCommandEventArgs e) +111
[more redundant trace info]
最佳答案
按照 GitHub repository 中的来源看起来 NPoco 中有一个错误:
你还没有指定什么类型id
是,但我假设它是一个 int 并且您有以下代码:
var id = 12345;
Delete<PurchaseItem>(id);
调用 NPoco Delete<T>(object pocoOrPrimaryKey)
,其代码为:
public int Delete<T>(object pocoOrPrimaryKey)
{
if (pocoOrPrimaryKey.GetType() == typeof(T))
return Delete(pocoOrPrimaryKey);
var pd = PocoDataFactory.ForType(typeof(T));
return Delete(pd.TableInfo.TableName, pd.TableInfo.PrimaryKey, null, pocoOrPrimaryKey); // This is the method your code calls
}
依次调用 NPoco Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
,其代码为:
public virtual int Delete(string tableName, string primaryKeyName, object poco, object primaryKeyValue)
{
if (!OnDeleting(new DeleteContext(poco, tableName, primaryKeyName, primaryKeyValue))) return 0;
var pd = PocoDataFactory.ForObject(poco, primaryKeyName);
...
}
我只包含了前两行,因为它是 PocoDataFactory.ForObject
根据您的堆栈跟踪抛出异常的方法。 ForObject(object o, string primaryKeyName)
的代码是:
public PocoData ForObject(object o, string primaryKeyName)
{
var t = o.GetType(); // This is where the exception comes from
...
}
这是正在发生的事情(假设 id 是 12345,表被映射为 PurchaseItem 并且主键被映射为 Id):
Delete<PurchaseItem>(pocoOrPrimaryKey : 12345);
Delete(tableName: "PurchaseItem", primaryKeyName: "Id", poco: null, primaryKeyValue: 12345);
PocoDataFactory.ForObject(o: null, primaryKeyName: Id);
Delete<PurchaseItem>("where id = @0", id);
的原因有效,是它遵循不同的代码路径,其中用于解析 PocoData
的类型来自typeof(T)
其中 T
是PurchaseItem
.
关于c# - NPoco:为什么我的 Delete<Model>() 调用会抛出 NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082377/
当我单步执行以下代码时,第二行的 report 为空。但是,第三行生成 NullReferenceException。 member this.setTaggedResearchReportList
我正在尝试按照建议从 URL 获取查询字符串 here ,但我收到 NullReferenceException。我的代码与链接帖子中的代码之间的唯一区别是我的代码是静态的,我看不出这会如何导致错误。
我有以下程序,这是一段示例代码,展示了 C# 反射如何对类进行操作。一切正常,完全没有问题。 public class Program { public static void Main() {
我在以下代码中使用 F# 类型和数据结构(我在 Mac 上使用 Monodevelop,这仅发生在 Interactive 中): type UnbalancedSet = | E |
我有一个 asp.net/C# 类可以调整图像的大小以作为文件缓存在服务器上,但是确定使用哪个编码器的代码部分似乎偶尔会抛出 NullReferenceException。 下面是初始化和传回编码器的
我已经向几位同事展示了这一点,但没有人给出解释。我纯粹偶然遇到了这个问题,因为我以为我在代码中发现了一个错误,但惊讶地发现代码实际上运行了。这是一个简化版本。这已通过 XE-2 完成。 到目前为止,与
这把我难住了。我试图优化 Noda Time 的一些测试,其中我们进行了一些类型初始值设定项检查。我想在将所有内容加载到新的 AppDomain 之前,我想找出类型是否有类型初始值设定项(静态构造函数
当我的页面加载时,我从 SQLite 数据库加载一个列表,有时在加载时我会得到 NullReferenceException ,并显示错误:对象引用未设置为对象的实例。 它在 SQLite 类文件中破
这个问题已经有答案了: What is a NullReferenceException, and how do I fix it? (26 个回答) 已关闭 4 年前。 我收到以下异常: Unhan
当我的页面加载时,我从 SQLite 数据库加载一个列表,有时当它加载时我得到 NullReferenceException 错误说 Object reference not set to an对象的
我有以下代码: try { using (var stream = new MemoryStream()) { var ms = stream; if (con
这个问题已经有答案了: What is a NullReferenceException, and how do I fix it? (26 个回答) 已关闭 4 年前。 我收到以下异常: Unhan
我有以下代码。 XElement opCoOptOff = doc.Descendants(ns + "OpCoOptOff").FirstOrDefault(); String opCo = o
这个问题在这里已经有了答案: What is a NullReferenceException, and how do I fix it? (27 个答案) 关闭 4 年前。 我有队列代码但抛出 N
所以我正在制作一个应用程序,一切都运行良好,直到发生以下情况: 我已经通过调试器进行了检查,但无法找到导致此异常的确切原因。我感觉根本原因在 SQL 方面,因为这是我最近更改的唯一部分,但我需要确切地
class Puzzle { private int PUZZLESIZE = 3; private int col, row; priva
我有一项学校作业,我快完成了,只剩下一件事了。每次启动程序时,我都会收到 NullReferenceException。除了抛出异常的 ListView 外,一切都按预期工作。 这是来自主窗体: pr
我向 Form1 组件添加了一个面板。该面板名为 panel1。 浏览通用列表时,我想动态添加标签。看看我的小代码: if (list.Count > 0) { foreach (TLClas
这是我的场景:我有一个控制台应用程序,我正在尝试使用以下代码处理控制台的“关闭事件”: static void Main(string[] args) { SetConsoleCtrlHand
这可能已经有人问过了。我搜索了 SO 并发现了一些关于 Null VS String.Empty 的问题,但我很好奇为什么以下语句不会抛出 NullReferenceException: String
我是一名优秀的程序员,十分优秀!