I am getting a null reference exception that I can't pin down.

By process of elimination using a variety of assumptions I've narrowed it down to a single variable, a linq iteration variable, like so:

var process = (from p in context.Processes where p.ProcessID == processID select p).FirstOrDefault();

Is it possible that p variable is null? If so why would SPMetal/the DataContext have given me a null list item?

Here is a list of all assumptions made, which have narrowed down my list of dereferences to this single instance:

  • Assume stack trace is accurate (no need to look further into called methods)
  • Assume types cannot be null
  • Assume constructors cannot return null
  • Assume linq queries do not return null (https://stackoverflow.com/questions/1191919/what-does-linq-return-when-the-results-are-empty)
  • Assume no internal method changes my variables to null (used VS's "find all references" feature to confirm)
  • Assume UserControls instantiated in the ascx cannot be null
  • Assume execution did not reach a certain point (where a list item would have been created)
  • Assume IssueRegister list was not removed or renamed since the page was last loaded, and that if it was, SPMetal generated code would have thrown an error instead of returning null
  • Assume SPContext.Current.Web does not return null (for a browser request to a page)
  • Have you tried setting up if-else statements to check if context.Processes is null or greater than 0?
    – user2536
    Jul 9, 2013 at 8:22
  • no but i have tried giving linq null in that part of the query, and it doesn't throw a NullReferenceException, it throws an ArgumentNullException, also I can guarantee that context.Processes is not null elsewhere in execution
    – Nacht
    Jul 10, 2013 at 1:17

1 Answer 1


Following are the possible scenarios:

  1. IDs do not match then result is null
  2. It happens if your list is hidden (sharepoint linq) can't see them
  3. Try this syntax:

    _controllerContext.Tasks.AsEnumerable<SharePointDAO.Task>().Where(tsk => tsk.Id == taskId).FirstOrDefault()

Hope it helps!

  • 1) an empty linq query never returns null, it always returns an empty IEnumerable, as per stackoverflow.com/questions/1191919/… 2) My list is not hidden, and I know that context.Processes is not null, as per my comment above 3) My question is about p, or in your case, tsk. That would not change anything.
    – Nacht
    Jul 10, 2013 at 1:21
  • Only as far as the 'Where' clause. As soon as you add 'FirstOrDefault()', you will get either the first item of the IEnumerable, or null if the IEnumerable doesn't contain any items. Could you try and split up your query? First get all processes, then do FirstOrDefault on the next line. If you debug then you should be able to tell whether A) the collection is empty or B) the where clause causes the exception. If it's the latter, see for which item the exception occurs. I don't see how 'p' or 'ProcessID' could be null, but maybe something happens in the constructor of Process. Jul 10, 2013 at 8:01
  • no... I do realise what FirstOrDefault() does, it is ok for process to be null. And unfortunately debugging won't help as I can't actually reproduce the problem. I only know the NullReferenceException came from this method, and I have by process of elimination narrowed it down to the p.ProcessID dereference. There are certainly other assumptions ive made that i could look at more carefully, but to me this is currently the most likely culprit.
    – Nacht
    Jul 10, 2013 at 23:32
  • see question update for a list of assumptions made...
    – Nacht
    Jul 11, 2013 at 1:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.