3

I have a Windows Service with a log file to write any exceptions caught. In a simple foreach using LINQ, I am getting an exception for the classic:

Object reference not set to an instance of an object.

However, when I run the code locally, by 'freezing' the Windows Service code and just running the code through a Main function, I can't seem to replicate this issue. The stack trace says the line causing the issue is 142.

var newTickets = tickets
    .Where(t => t != null)
    .Where(t => (DateTime)t.LastActivityDate >= lastAutoTaskSync); 

foreach(Ticket ticket in newTickets) //Line 142

Strangely, I have checked everything possible I could think it could be, by adding the following lines:

var newTickets = tickets
    .Where(t => t != null)
    .Where(t => (DateTime)t.LastActivityDate >= lastAutoTaskSync);

if (newTickets == null) throw new Exception("newTickets is null");
if (tickets == null) throw new Exception("tickets is null");
if (lastAutoTaskSync == null) throw new Exception("date is null");

foreach (Ticket ticket in newTickets)

And again, the same line throws the same exception.

Is there anything that I may be missing, or something that could cause this exception? I thought it may be the code inside the LINQ, such as LastActivityDate being null, but if that were the case, surely this would be the line to throw the exception, not the foreach itself?

To add, the code runs around 50% of the time, running through the foreach with no issues. For example, the service log indicates that the code in the loop runs 3 or 4 times, and then the exception hits on the fifth.

14
  • 5
    Remember that IEnumerable isn't evaluated until you start using it. My guess, t.LastActivityDate is null in the first element of the array. You could check this by adding a .ToList() to var newTickets and see if the error moves there. Commented May 18, 2016 at 9:51
  • 2
    Is your code optimised? If so, the line number from the exception details may be incorrect. Commented May 18, 2016 at 9:53
  • 2
    @JeffFoster: if LastActivityDate is a nullable DateTime and some are null he'd get a System.InvalidOperationException instead on the cast to DateTime. Commented May 18, 2016 at 9:56
  • What is tickets? Is this an IQueryable that itself executes some other lambdas that may cause the exception? Commented May 18, 2016 at 10:00
  • @codran The line numbers are reporting correctly yeah. I threw an exception above all this to double check, and they're reporting correctly. @JeffFoster As Tim said, I was relying on the cast to pick up on this. All properties on the object are object type. I have added a .ToList() and have put it back on the test server. It may be worth noting that this code runs half the time with no issues and performs the functions coded into it. Commented May 18, 2016 at 10:01

1 Answer 1

1

The stack trace was reporting a wrong line number, causing me to believe the LINQ query was causing the exception. In reality, the exception was being thrown within the foreach loop, a few lines down, where I try to access an object within a Ticket's properties, which was null.

Lesson learned, don't always believe in and rely on the stack trace entirely!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.