2013-06-28 28 views
5

當我運行下面的代碼時,我得到一個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一個不同的異常。

任何人都能看到我失蹤的東西嗎?

+1

放一個斷點並檢查所有的對象。如果我不得不猜測,根據你對CategoryId的描述,「fogbugzCase.Category」可能爲空。但檢查一切。如果你訪問一個空引用的屬性,你會得到一個'NullReferenceException'。 – zimdanen

+0

@zimdanen thx,有東西是空的 - 只是試圖找到方法來檢查現在。 –

+1

幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –

回答

6

聽起來像其中一個屬性是null。拆分代碼:

var args = 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}); 
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);", args); 

它的代碼在第一行失敗,它不是任何與短小精悍的 - 它甚至沒有接近短小精悍的任何地方。你需要檢查所有的嵌套成員。

+0

工作,thx。現在我得到一個NotSupportedException,我剛剛問到[這裏](http://stackoverflow.com/questions/17375134/notsupportedexception-when-inserting-with-dapper) –

相關問題