1

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.TableQuery1[ShoppingAssistant.ShoppingList].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1 queryArgs) [0x00000] in :0

at SQLite.TableQuery1[ShoppingAssistant.ShoppingList].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List1 queryArgs) [0x00000] in :0

at SQLite.TableQuery1[ShoppingAssistant.ShoppingList].GenerateCommand (System.String selectionList) [0x00000] in <filename unknown>:0 at SQLite.TableQuery1[ShoppingAssistant.ShoppingList].GetEnumerator () [0x00000] in :0

at System.Collections.Generic.List1[ShoppingAssistant.ShoppingList].AddEnumerable (IEnumerable1 enumerable) [0x00000] in :0

at System.Collections.Generic.List1[ShoppingAssistant.ShoppingList]..ctor (IEnumerable1 collection) [0x00000] in :0

at 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

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Guano
  • 139
  • 1
  • 9
  • Have you considered the possibility that an object reference is `null`? – CL. Nov 23 '14 at 10:10
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CL. Nov 23 '14 at 10:12
  • It has been fixed, It turned out that sqlite wants the objects that are inserted into the db not instantiated in a non empty constructor, as i turned out the string Name is set there so it was null when running the code, thanks for the suggestions. How do i upvote you guys? – Guano Nov 24 '14 at 12:44
  • my issue was that the DataContext was not added in the default constructor and therefore will always be null. – Jephren Naicker Feb 24 '23 at 14:51

0 Answers0