SQLite.net table query throws NullReferenceException SQLite.net table query throws NullReferenceException sqlite sqlite

SQLite.net table query throws NullReferenceException


I'm pretty sure you are not working in this project anymore, but I'm going to answer it because it may help someone which still is struggling with this problem.

The problem is your entity and I don't know what exactly is the problem because you didn't post your class here. But basically you probably were trying to access a property that wasn't fetch from your database yet. For example, I have a class with two properties:

public class MyClass{      public DateTime Date { get; set; } // You're saving this property in the database      public bool IsLate => Date < DateTime.Today; // This property is not be saving, and probably is the cause of your exception}

I don't know why SQLite does that, but when it is querying your entities, if in your query you're filtering by IsLate property, it's going to crash because Date property wasn't fetch yet.

For solving this you'll have to fetch the entities first, then filter by this property. That's basically what you did in EDIT 2. You've fetch all entities then filtered.


Maybe I am too new to know what I am talking about, but I think your last example just needed an await statement before your ToListAsync call.

var query = Connection.Table<WorkDay>().Where(wd => wd.Date >= @from && wd.Date <= to);return await query.ToListAsync().ConfigureAwait(false);

Not sure on the specifics of the first problem, but I would think that it has something to do with the TableAsyncQuery object, that the Where statement produces, not being fully initialized before the ToListAsync calls it on another thread.


adding bit more details to Daniel Cunha answeri have this in my cProduct class

    [PrimaryKey, AutoIncrement]    public int rowid    {        get => _rowid;        set => _rowid = value;    }    [Ignore]    public int ID => rowid; // just for convinience

this query works product.Product = dbConnection.Table<cProduct>().Where(s => s.rowid == product.ProductID).FirstOrDefault();

but this fails with null reference exception

product.Product = dbConnection.Table<cProduct>().Where(s => s.ID == product.ProductID).FirstOrDefault();

s.ID can't be use in db query.. there is not such field in the table.