2013-06-28 12 views
5

Po uruchomieniu następującego kodu otrzymuję wyjątek NullReferenceException, który mówi, że odwołanie do obiektu nie jest ustawione na wystąpienie obiektu. Pomyślnie wstawiłem dappera przy użyciu mniej skomplikowanych obiektów, ale w tym samym formacie, więc nie jestem pewien, co robię źle.Wyjątek NullReferenceException podczas wstawiania z Dapper

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}); 
     } 
    } 
} 

raz pierwszy próbował robić prostszą drogę tylko przejazdem w fogbugzCase zamiast anonimowego obiektu, ale spowodowało innego wyjątku o IDkategorii.

Ktoś widzi, czego mi brakuje?

+1

Umieść punkt przerwania i sprawdź wszystkie swoje obiekty. Gdybym musiał zgadnąć, na podstawie tego, co powiedziałeś o CategoryId, 'fogbugzCase.Category' może mieć wartość null. Ale sprawdź wszystko. Jeśli uzyskasz dostęp do właściwości pustego odwołania, otrzymasz wyjątek "NullReferenceException". – zimdanen

+0

@zimdanen thx, było coś pustego - tylko próbowałem znaleźć sposób, by to sprawdzić. –

+1

Prawie wszystkie przypadki wyjątku "NullReferenceException" są takie same. Zobacz "[Co to jest wyjątek NullReferenceException w .NET?] (Http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" w przypadku niektórych wskazówek. –

Odpowiedz

6

Wygląda na to, że jedną z właściwości jest null. Podzielić kod:

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); 

to kod nie działa na pierwszej linii, nie ma nic wspólnego z Dapper - nawet nie zbliżyć Dappera. Musisz sprawdzić wszystkich zagnieżdżonych członków.

+0

który działał, thx. Teraz otrzymuję wyjątek NotSupportedException, o którym właśnie pytałem [tutaj] (http://stackoverflow.com/questions/17375134/notsupportedexception-when-inserting-with-dapper) –

Powiązane problemy