Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1273

Find() fails with "Object reference not set to an instance of an object."

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.4.1
    • Affects Version/s: 2.0
    • Component/s: API
    • None
    • Environment:
      VS2013, Windows 7

      The following code fails with NullReference exception:

      int? key = null;
      collection.Find(r => r.RequestId == requestId && (key == null || r.Key == key)).Limit(1).FirstOrDefaultAsync();
      
      // Class, stored in collection
      public class Request
      {
         public string RequestId {get; set;}
         public int Key {get; set;}
      }
      

      Stack trace attached.

      Apparently, the problem is that the serializer selected based on the document property type, which is INT, rather than on the actual value type, which is INT?. Therefore, it later fails on NULL value, hence serializer expects ValueType, rather than reference type.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            iiwaasnet Sergey Ivasenko
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: