3

I'm using sqlite-net and sqlite-net-extensions for the blob in a Xamarin.Android project. I have this schema:

    public enum TemplateCategories
    {
        Emojis,
        Stars,
        Hearts,
        Characters,
        Emotions,
    }

public class Templates
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int Category { get; set; }
    [TextBlob("imagesBlobbed")]
    public List<int> Images { get; set; }
    public string imagesBlobbed { get; set; }
}

and i'm trying to add a Templates object with this:

    public static void CreateDB(SQLiteConnection db)
    {
        db.CreateTable<Templates>();
        AddTemplate(
            db,
            (int)TemplateCategories.Emojis,
            new List<int>
            {
            Resource.Drawable.ic_angry,
            Resource.Drawable.ic_sad,
            Resource.Drawable.ic_neutral,
            Resource.Drawable.ic_happy,
            Resource.Drawable.ic_smiling,
            }
        );

    public static void AddTemplate(SQLiteConnection db, int category, List<int> images)
    {
        var alreadyExist = db.Table<Templates>().Where(record => record.Images == images);

        if (alreadyExist?.Count() == 0)
        {
            var template = new Templates()
            {
                Category = category,
                Images = images,
            };
            db.InsertWithChildren(template, recursive: true);
        }
    }

but at AddTemplate im getting null reference exception in line var alreadyExist . I have look the documentation and i'm not sure how i can search for a specific item (List blob in my case) and retrieve it.

I just want to be sure first that the data that im inserting are not already in the DB.

Is my approach wrong? Since i want to be able to query the List Images should i create an OneToMany relationship with another table that will hold the Images instead of a blob?

Exception:

**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

03-08 00:38:28.681 D/Mono    (20388): DllImport attempting to load: '/system/lib/liblog.so'.
03-08 00:38:28.682 D/Mono    (20388): DllImport loaded library '/system/lib/liblog.so'.
03-08 00:38:28.682 D/Mono    (20388): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
03-08 00:38:28.682 D/Mono    (20388): Searching for '__android_log_print'.
03-08 00:38:28.682 D/Mono    (20388): Probing '__android_log_print'.
03-08 00:38:28.682 D/Mono    (20388): Found as '__android_log_print'.
03-08 00:38:28.686 I/MonoDroid(20388): UNHANDLED EXCEPTION:
03-08 00:38:28.690 I/MonoDroid(20388): System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x008b5] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x0009d] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].GenerateCommand (System.String selectionList) [0x0005f] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at SQLite.TableQuery`1[T].Count () [0x00000] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.Helpers.TemplateDB.AddTemplate (SQLite.SQLiteConnection db, System.Int32 category, System.Collections.Generic.List`1[T] images) [0x00077] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:25 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.Helpers.TemplateDB.CreateDB (SQLite.SQLiteConnection db) [0x00009] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:68 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainApplication.GetTemplateById (System.Int32 id) [0x00025] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainApplication.cs:43 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainActivity.SetButtonImages () [0x0008c] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:554 
03-08 00:38:28.690 I/MonoDroid(20388):   at RateMe.MainActivity.OnResume () [0x0000f] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:507 
03-08 00:38:28.690 I/MonoDroid(20388):   at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <985f5179c1dd4b3b98fb1b8dcc1ee9be>:0 
03-08 00:38:28.690 I/MonoDroid(20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:28.707 E/AppCenterCrashes(20388): Unhandled Exception from source=AndroidEnvironment
03-08 00:38:28.707 E/AppCenterCrashes(20388): System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x008b5] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].CompileExpr (System.Linq.Expressions.Expression expr, System.Collections.Generic.List`1[T] queryArgs) [0x0009d] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].GenerateCommand (System.String selectionList) [0x0005f] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at SQLite.TableQuery`1[T].Count () [0x00000] in <84b9c9e630fa45bd8ac799333976ebbf>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.Helpers.TemplateDB.AddTemplate (SQLite.SQLiteConnection db, System.Int32 category, System.Collections.Generic.List`1[T] images) [0x00077] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:25 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.Helpers.TemplateDB.CreateDB (SQLite.SQLiteConnection db) [0x00009] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\Helpers\TemplateDB.cs:68 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainApplication.GetTemplateById (System.Int32 id) [0x00025] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainApplication.cs:43 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainActivity.SetButtonImages () [0x0008c] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:554 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at RateMe.MainActivity.OnResume () [0x0000f] in C:\Users\Exoskeletor\Documents\Projects\RateMe\RateMe\MainActivity.cs:507 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at Android.App.Activity.n_OnResume (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <985f5179c1dd4b3b98fb1b8dcc1ee9be>:0 
03-08 00:38:28.707 E/AppCenterCrashes(20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:28.714 D/Mono    (20388): Loading reference 5 of /storage/emulated/0/Android/data/com.cdrosos.simplecustomerfeedback/files/.__override__/Microsoft.AppCenter.Crashes.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
03-08 00:38:28.714 D/Mono    (20388): Assembly Ref addref Microsoft.AppCenter.Crashes[0x9d908ac0] -> System.Core[0x872b3280]: 8
03-08 00:38:28.744 D/Mono    (20388): DllImport searching in: '__Internal' ('(null)').
03-08 00:38:28.744 D/Mono    (20388): Searching for 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.744 D/Mono    (20388): Probing 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.744 D/Mono    (20388): Found as 'java_interop_jnienv_new_local_ref'.
03-08 00:38:28.782 W/zygote  (20388): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
03-08 00:38:28.786 D/Mono    (20388): DllImport searching in: '__Internal' ('(null)').
03-08 00:38:28.786 D/Mono    (20388): Searching for 'java_interop_jnienv_throw'.
03-08 00:38:28.786 D/Mono    (20388): Probing 'java_interop_jnienv_throw'.
03-08 00:38:28.786 D/Mono    (20388): Found as 'java_interop_jnienv_throw'.
**System.NullReferenceException:** 'Object reference not set to an instance of an object.'

03-08 00:38:30.941 E/mono-rt (20388): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
03-08 00:38:30.941 E/mono-rt (20388):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
03-08 00:38:30.941 E/mono-rt (20388):   at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.13(intptr,intptr)
2
  • 1
    public List<int> Images { get; set; } = new List<int>(); Commented Mar 7, 2020 at 22:25
  • 1
    thanks for the comment, i still see the null reference exception Commented Mar 7, 2020 at 22:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.