SQLite.net throws a NullReferenceException request

I use the following line of code to query some data from a given table in a SQLite database (WP81 platform, but I think it does not matter here).

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

When I execute my code, I get a NullReferenceException in the Where clause. When I remove the where clause everything works fine.

 return await Connection.Table<WorkDay>().ToListAsync().ConfigureAwait(false); 

To make sure all the records in my table are valid and that the Date column is not null, I used the SQL tool to search the SQLite database.

Since I cannot debug lambda expressions, I seem to be fixated on how to find the problem here. My guess is that something will go wrong due to asynchronous processing.

Edit: Here is the exact stacktrace exception

 {System.NullReferenceException: Object reference not set to an instance of an object. at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs) at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs) at SQLite.Net.TableQuery`1.CompileExpr(Expression expr, List`1 queryArgs) at SQLite.Net.TableQuery`1.GenerateCommand(String selectionList) at SQLite.Net.TableQuery`1.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at SQLite.Net.Async.AsyncTableQuery`1.<ToListAsync>b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at TimeStamp.Core.Services.DataService.<GetWorkDays>d__c.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at TimeStamp.Core.ViewModel.MainViewModel.<LoadWorkDays>d__1a.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)} 

Edit 2:

I played a little and understood the following.

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

When this statement is executed, it actually breaks down in the ToListAsync () method instead of the Where method. However, this does not help either.

Later I tried the following, which really works.

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

So, I did to separate the Where method actually. But although it works for me, it does not answer my question, because I still wonder why it does not work.

+7
c # sqlite async-await sqlite-net
source share
1 answer

I may be too new to know what I'm talking about, but I think your last example just needs an await statement before calling ToListAsync .

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

Not sure about the specifics of the first problem, but I would have thought that this has something to do with the TableAsyncQuery object, which expresses the Where statement, rather than being fully initialized before ToListAsync calls it in another thread.

+1
source share

All Articles