Search code examples
android-sqliteandroid-cursor

Add a row to Sqlite crashes if use getResources.getString() to add string value


I'm trying to add a row to Sqlite. At the beggining I was adding the row like this : (worked)

sqlDB.execSQL("INSERT INTO Family VALUES (" + index_family + ",'" + R.string.f1_pavimentos + "','" + (R.string.f0_descripcionPropamsa + "','" + R.drawable.color_pavimento + "');");

But was not good idea, beacause it only saves de id of the string, and I want the value. So I decided to do:

sqlDB.execSQL("INSERT INTO Family VALUES (" + index_family + ",'" + context.getResources().getString(R.string.f1_pavimentos )+ "','" + context.getResources().getString(R.string.f0_descripcionPropamsa )+ "','" + R.drawable.color_pavimento + "');");

adding the getResources.getString() but it crashes sending me this error:

07-16 10:42:26.200    1746-1746/com.propamsa E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.propamsa, PID: 1746
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.propamsa/com.propamsa.listas.FamilyList}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at com.propamsa.basededatos.ProductDBHandler.insertInformation(ProductDBHandler.java:183)
            at com.propamsa.basededatos.ProductDBHandler.onCreate(ProductDBHandler.java:57)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at com.propamsa.basededatos.ProductDBHandler.getAllFamiliesCursor(ProductDBHandler.java:109)
            at com.propamsa.listas.FamilyList.onCreate(FamilyList.java:39)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)

And can see that is caused by NullPointer, but the string exist and is well spelled.

How can I save the string of it?


Solution

  • Possibly, your context is null.
    Try using getApplicationContext() instead of context