From this code a Null exception is throw and I'm pusled as to why that is. The line that throws the NullReferenceException is this one,
if (await dbConn.FindAsync<ShoppingList>(x => x.Name == insertedList.Name) != null)
Here is the whole method:
public async Task<string> SaveSingleShoppingList(IShoppingList shoppingList)
{
try
{
var dbConn = new SQLiteAsyncConnection(dbPath);
ShoppingList insertedList = (ShoppingList)shoppingList;
await SaveGroceries(shoppingList.Groceries, shoppingList.Name, dbConn);
if(await dbConn.FindAsync<ShoppingList>(x => x.Name == insertedList.Name) != null)
await dbConn.UpdateAsync(insertedList);
else
await dbConn.InsertAsync(insertedList);
return "Shopping list Saved!";
}
catch (SQLiteException ex)
{
return ex.Message;
}
catch(NullReferenceException nre)
{
return nre.Message;
}
}
Below is the full extent of the exception message
{System.NullReferenceException: Object reference not set to an instance of an object at SQLite.TableQuery
1[ShoppingAssistant.ShoppingList].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 queryArgs) [0x00000] in :0at SQLite.TableQuery
1[ShoppingAssistant.ShoppingList].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List
1 queryArgs) [0x00000] in :0at SQLite.TableQuery
1[ShoppingAssistant.ShoppingList].GenerateCommand (System.String selectionList) [0x00000] in <filename unknown>:0 at SQLite.TableQuery
1[ShoppingAssistant.ShoppingList].GetEnumerator () [0x00000] in :0at System.Collections.Generic.List
1[ShoppingAssistant.ShoppingList].AddEnumerable (IEnumerable
1 enumerable) [0x00000] in :0at System.Collections.Generic.List
1[ShoppingAssistant.ShoppingList]..ctor (IEnumerable
1 collection) [0x00000] in :0at System.Linq.Enumerable.ToList[ShoppingList] (IEnumerable`1 source) [0x00000] in :0
at SQLite.TableQuery`1[ShoppingAssistant.ShoppingList].FirstOrDefault () [0x00000] in :0
at SQLite.SQLiteConnection.Find[ShoppingList] (System.Linq.Expressions.Expression`1 predicate) [0x00000] in :0
at SQLite.SQLiteAsyncConnection+c__AnonStorey7`1[ShoppingAssistant.ShoppingList].<>m__0 () [0x00000] in :0
at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[ShoppingAssistant.ShoppingList].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in :0
at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in :0 at System.Threading.Tasks.Task.ThreadStart () [0x00000] in :0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0 at System.Runtime.CompilerServices.TaskAwaiter`1[ShoppingAssistant.ShoppingList].GetResult () [0x00000] in :0 at ShoppingAssistant.SQLiteDataAcces+d__2d.MoveNext () [0x0009d] in c:\Users\kaj\Desktop\dbTest\dbTest\SQLiteDataAcces.cs:187 } System.NullReferenceException