tags:

views:

57

answers:

1

I have code that's working beautifully on my development machine, but when deployed to the server is throwing null reference exception. So, I can't step through the code, but I've pinned down the culprit. But now I am puzzled. Here's the code. The question is below.

Dim certs = From p In persons _
        Select New Certificate( _
        p.Value, _
        New CertificateData.Seminar(thisEvent.Seminar.Name, _
               thisEvent.StartDate.Value, _
               thisEvent.EndDate.Value, _
               thisEvent.Venue.City, _
               thisEvent.Venue.State, _
               New CertificateData.Instructor( _
               staffMember.Name, _
               staffMember.Titles, _
               instrSignatPath))) _
        With {.CertificateId = p.Key}

lblMessage.Text = CStr(certs Is Nothing)
lblMessage.Text = lblMessage.Text + "<br />" + CStr(certs.Count())

In the code above persons is a dictionary of custom class, and certs is of IEnumerable type. Now here's the quandary.. The first line that sets the label returns False, so certs is not null. But the second line throws an null reference exception. How is this possible?

+1  A: 

My guess is because of the deferred execution of LINQ. The items are enumerated only when you call Count() and if your code in the expression fails, you'll get the exception right there.

I'm pretty sure that at least one of staffMember, thisEvent, thisEvent.Seminar or thisEvent.Venue are null.

Lucero
Good Idea!.....
Marcel
Thank you. You were right on. thisEvent.Venue was null
Antony Highsky
@Draak, thanks for the feedback. The deferred execution is quite often causing things to happen in a different way than initially anticipated by the programmer, and therefore I's always a good idea to ask yourself whether deferred execution could be causing your trouble when dealing with LINQ problems.
Lucero