2

I have a DbHelper class that extend SQLiteOpenHelper and trying to create a db upgrade system on the method onUpgrade(), but during the execution of the very first query it trhow a NullPointerException

My onUpgrade code is:

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        SysLog("upgrading Db from "+oldVersion+" to "+newVersion);
        if (db == null){
            ErrorLog("Errore nel Db", "db is null");
            $.finish();
            return;
        }
        db.beginTransaction();
        try {
            int i = oldVersion;
            if (oldVersion != newVersion){
                exportDB();
            }
            while(i < newVersion){
                ArrayList<String> qs = getSqlAlterDb(i);
                for (int z = 0; z < qs.size(); z++){
                    SysLog(qs.get(z));
                    db.execSQL(qs.get(z));
                }
                i++;
            }
            db.setTransactionSuccessful();
        }catch (Exception e){
            ErrorLog(e);
        }finally {
            if (db.inTransaction()) {
                db.endTransaction();
            }
        }

And this is my error log:

W/System.err: ---------[|__java.lang.NullPointerException]
W/System.err: ---------[    |__android.database.DatabaseUtils.getSqlStatementType(DatabaseUtils.java:1363)]
W/System.err: ---------[    |__android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1781)]
W/System.err: ---------[    |__android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1725)]
W/System.err: ---------[    |__wproject.barcodegest.db.DbHelper.onUpgrade(DbHelper.java:177)]
W/System.err: ---------[    |__android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)]
W/System.err: ---------[    |__android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)]
W/System.err: ---------[    |__wproject.barcodegest.db.DbManager.query(DbManager.java:306)]
W/System.err: ---------[    |__wproject.barcodegest.widget.user.UserGestorObj.inizialize(UserGestorObj.java:76)]
W/System.err: ---------[    |__wproject.barcodegest.widget.user.UserGestorObj.<init>(UserGestorObj.java:57)]
W/System.err: ---------[    |__wproject.barcodegest.MainActivity.onCreate(MainActivity.java:97)]
W/System.err: ---------[    |__android.app.Activity.performCreate(Activity.java:5372)]
W/System.err: ---------[    |__android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)]
W/System.err: ---------[    |__android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)]
W/System.err: ---------[    |__android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)]
W/System.err: ---------[    |__android.app.ActivityThread.access$700(ActivityThread.java:159)]
W/System.err: ---------[    |__android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)]
W/System.err: ---------[    |__android.os.Handler.dispatchMessage(Handler.java:99)]
W/System.err: ---------[    |__android.os.Looper.loop(Looper.java:176)]
W/System.err: ---------[    |__android.app.ActivityThread.main(ActivityThread.java:5419)]
W/System.err: ---------[    |__java.lang.reflect.Method.invokeNative(Native Method)]
W/System.err: ---------[    |__java.lang.reflect.Method.invoke(Method.java:525)]
W/System.err: ---------[    |__com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)]
W/System.err: ---------[    |__com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)]
W/System.err: ---------[    |__dalvik.system.NativeStart.main(Native Method)]

I have already tested the version, if db is null or if the query is null. I have everything that is required or that i know how to test. I'm sure qs.get() return a coerent query. And i'm sure db is not null. As the log says, the problem seems to come from the DatabaseUtils when i try to get the state of the db.

Zoronel
  • 35
  • 6
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OBX Oct 06 '16 at 15:35
  • `qs.get(z)`... What is it? Seems to be null – OneCricketeer Oct 06 '16 at 15:36
  • Post your query – Bahu Oct 06 '16 at 15:36
  • please print content of qs.get(z) looks like you are getting a null object – Amod Gokhale Oct 07 '16 at 10:09
  • The above line, (SysLog(qs.get(z));) is my custom system log and don't give null, correctly print a coerent query. Also the null pointer come from DatabaseUtils.java:1363. If qs.get return null it must come from DbHelper, or i'm wrong? – Zoronel Oct 10 '16 at 08:00
  • 1
    @Zoronel Yes, wrong: The JVM wouldn't throw a NPE if you pass a `null` as a parameter; that's legal. It only would throw NPE if you try to de-reference a null reference: I.E.: if you try to invoke some method on a null reference. – Little Santi Oct 10 '16 at 08:30

1 Answers1

1

According with the source code of DatabaseUtils.getStatementType, the only possible cause of a NPE in that method would be that the received parameter sql is null. Review your qs list's contents, and try some defensive programming to ensure that no nulls are sent as a parameter:

String sql=qs.get(z);
if (sql!=null)
{
    db.execSQL(sql);
}
Little Santi
  • 7,940
  • 2
  • 14
  • 40