0

A first chance exception of type 'System.NullReferenceException' occurred in .dll. I cannot figure out why all.FindAll(...) throws this exception every time. Is the reason because the lambda expression is not short circuiting the AND OR conditions which is causing the Name, BillingCity, and or BillingStreet to cause this? Also I'm open to a better approach (like a working one =)) to my current solution.

    public async Task<JsonResult> AutoCompleteSearch(string term)
    {
        // Filter accounts
        List<Account> all = await GetAccounts();
        List<Account> filtered = new List<Account>();

        filtered = all.FindAll(e => 
            ((e.Name != null) && e.Name.ToLower().Contains(term.ToLower())) ||
            ((e.BillingCity != null) && e.BillingCity.ToLower().Contains(term.ToLower())) ||
            ((e.BillingStreet != null) && e.BillingState.ToLower().Contains(term.ToLower())));

        return Json(filtered, JsonRequestBehavior.AllowGet);
    }
  • 8
    `(e.BillingStreet != null) && e.BillingState`. You're checking for null on the wrong property. – crush Jan 29 '14 at 15:43
  • wow! ty... sorry for wasting your time and ty! –  Jan 29 '14 at 15:45
  • @crush you should respond with this as the answer since you were first to resolve this. That way I can give points. –  Jan 29 '14 at 15:58

2 Answers2

1

You appear to have a simple typo, probably from intellisense autocompletion.

On this line:

((e.BillingStreet != null) && e.BillingState.ToLower().Contains(term.ToLower())));

You check BillingStreet for null, but then attempt to access BillingState property. One of these is probably not what you meant.

crush
  • 16,713
  • 9
  • 59
  • 100
0

You never null check e.BillingState. Add

&& e.BillingState

before

&& e.BillingState.ToLower()
Matthew Beatty
  • 2,722
  • 2
  • 14
  • 16
  • Thank you @MBeatty I would have marked this as the answer, but crush answered this first in a comment. Thank you again. –  Jan 29 '14 at 16:01