Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 (http://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)
share|improve this question
    
Have you tried setting up if-else statements to check if context.Processes is null or greater than 0? –  Daniel Ziga Jul 9 '13 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 '13 at 1:17
add comment

1 Answer

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!

share|improve this answer
    
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 '13 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. –  Spongeroberto Squarepantalones Jul 10 '13 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 '13 at 23:32
    
see question update for a list of assumptions made... –  Nacht Jul 11 '13 at 1:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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