Litedb: collection.Find(Query.Contains(Field, Value)) raises NullPointerException

Created on 14 Jul 2016  路  3Comments  路  Source: mbdavid/LiteDB

I have this really weird bug using LiteDB [1.0.4], when I try to make a Find that uses Contains I get a NullPointerException for every query but one.
My document is like this:

public class Index
    {
        public string FileId { get; set; }
        public string AgreementId { get; set; }
        public string AccountNumber { get; set; }
        public string SortCode { get; set; }
        public string FilePath { get; set; }
    }

And my queries are as so:

var col = _db.GetCollection<Index>("index");
List<Index> query = new List<Index>();

switch (searchCriteria)
{
   case "File ID":
       query = col.Find(Query.Contains("FileId", searchText)).ToList();
       break;
    case "Agreement ID":
       query = col.Find(Query.Contains("AgreementId", searchText)).ToList();
       break;
   case "Account Number":
       query = col.Find(Query.Contains("AccountNumber", searchText)).ToList();
       break;
   case "Sort Code":
       query = col.Find(Query.Contains("SortCode", searchText)).ToList();
       break;
   case "Account Number & Sort Code":
       query = col.Find(Query.And(Query.EQ("AccountNumber", searchText), Query.EQ("SortCode", searchTextOpt))).ToList();
       break;
   default:
       break;
}

The only one that goes though is with FileID, otherwise I get this exception.
Every field is EnsureIndex, I built the database several times.
This error also happens in the LiteDB Shell when doing something like:
db.index.find SortCode contains "59

bug

Most helpful comment

Hi @manny42, yes, it麓s a bug and was fixed, but I think that was publish only in v2-beta/rc. Is just a "half-line" fix :) [QueryContains.cs -> .Where(x => x.Key.IsString && x.Key.AsString.Contains(v))

This weekend I will generate 1.0.5 version to fix this.

All 3 comments

Adding the stacktrace if it can help

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=LiteDB
  StackTrace:
       at LiteDB.QueryContains.<>c__DisplayClass1.<ExecuteIndex>b__0(IndexNode x)
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at LiteDB.LiteCollection`1.<Find>d__9.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at FasterPaymentsCOT.MainWindow.PerformSearch() in C:\Users\xxxx\MainWindow.xaml.cs:line 74

Hi @manny42, yes, it麓s a bug and was fixed, but I think that was publish only in v2-beta/rc. Is just a "half-line" fix :) [QueryContains.cs -> .Where(x => x.Key.IsString && x.Key.AsString.Contains(v))

This weekend I will generate 1.0.5 version to fix this.

Now it's fixed in v1.0.5 - it's on nuget

Was this page helpful?
0 / 5 - 0 ratings

Related issues

axelgenus picture axelgenus  路  3Comments

GW-FUB picture GW-FUB  路  3Comments

dangershony picture dangershony  路  3Comments

thecodrr picture thecodrr  路  4Comments

MoamenMohamed picture MoamenMohamed  路  4Comments