當我運行下面的代碼時,我得到一個NullReferenceException,表示沒有將對象引用設置爲對象的一個實例。我已經成功地使用較簡單的對象插入了精簡版,但格式相同,所以我不知道我在做什麼錯誤。用Dapper插入時出現NullReferenceException
public void Foo(IEnumerable<FogbugzCase> cases)
{
// using a singleton for the SqlConnection
using (SqlConnection conn = CreateConnection())
{
foreach (FogbugzCase fogbugzCase in cases)
{
conn.Execute("INSERT INTO fogbugz.Cases(CaseId, Title, ProjectId, CategoryId, Root, MilestoneId, Priority, Status, EstimatedHours, ElapsedHours, AssignedTo, ResolvedBy, IsResolved, IsOpen, Opened, Resolved, Uri, ResolveUri, OutlineUri, SpecUri, ParentId, Backlog) VALUES(@BugId, @Title, @ProjectId, @CategoryId, @RootId, @MilestoneId, @Priority, @StatusId, @EstimatedHours, @ElapsedHours, @PersonAssignedToId, @PersonResolvedById, @IsResolved, @IsOpen, @Opened, @Resolved, @Uri, @ResolveUri, @OutlineUri, @Spec, @ParentId, @Backlog);", new {BugId = fogbugzCase.BugId, Title = fogbugzCase.Title, ProjectId = fogbugzCase.Project.Id, CategoryId = fogbugzCase.Category.Id, RootId = fogbugzCase.Root, MilestoneId = fogbugzCase.Milestone.Id, Priority = fogbugzCase.Priority, StatusId = fogbugzCase.Status.Id, EstimatedHours = fogbugzCase.EstimatedHours, ElapsedHours = fogbugzCase.ElapsedHours, PersonAssignedToId = fogbugzCase.PersonAssignedTo.Id, PersonResolvedById = fogbugzCase.PersonResolvedBy.Id, IsResolved = fogbugzCase.IsResolved, IsOpen = fogbugzCase.IsOpen, Opened = fogbugzCase.Opened, Resolved = fogbugzCase.Resolved, Uri = fogbugzCase.Uri, OutlineUri = fogbugzCase.OutlineUri, Spec = fogbugzCase.Spec, ParentId = fogbugzCase.ParentId, Backlog = fogbugzCase.Backlog});
}
}
}
我第一次嘗試做的只是傳遞fogbugzCase
,而不是匿名對象的簡單方式,但導致約的CategoryId一個不同的異常。
任何人都能看到我失蹤的東西嗎?
放一個斷點並檢查所有的對象。如果我不得不猜測,根據你對CategoryId的描述,「fogbugzCase.Category」可能爲空。但檢查一切。如果你訪問一個空引用的屬性,你會得到一個'NullReferenceException'。 – zimdanen
@zimdanen thx,有東西是空的 - 只是試圖找到方法來檢查現在。 –
幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –