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

mongodb-csharp-driver throwing NullReferenceException when using CountAsync

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.0
    • Affects Version/s: 2.0
    • Component/s: API

      REF: https://groups.google.com/forum/#!topic/mongodb-user/yfbzkozHS64

      Hi,

      Perhaps I'm not use this method properly but I think I am. Here is my code:

      IMongoCollection<TType> c = _database2.GetCollection<TType>("MyDocuments");
      var f1 = new FilterDefinitionBuilder<TType>();
      var fluent = c.Find(f1.Text(queryFilter.SearchTerm));
      var result = fluent.CountAsync();
      

      When CountAsync executes (I normally would attach a .Result, but I wanted to isolate the issue here), it throws the following exception:

      Object reference not set to an instance of an object.
      at MongoDB.Driver.FindFluent`2.CountAsync(CancellationToken cancellationToken)
      at ProfileUnity.Mongo.Db.MongoDatabaseManager.<>c__DisplayClass9`1.<GetAll>b__8() in d:\work\proudotnet\src\ProfileUnity.Mongo.Db\MongoDatabaseManager.cs:line 192
      

      I should mention that when I comment out the CountAsync() and replace it with:

      var result = fluent.ToListAsync().Result

      My correct results are returned. Any advice?

      Thanks

            Assignee:
            craig.wilson@mongodb.com Craig Wilson
            Reporter:
            jeremyh Jeremy Herman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: