我有一个DbHelper类,它扩展了SQLiteOpenHelper并尝试在方法 onUpgrade()上创建数据库升级系统,但是在执行第一个查询时它会显示一个NullPointerException
我的onUpgrade代码是:
@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();
}
}
这是我的错误日志:
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)]
如果db为null或查询为null,我已经测试了该版本。我拥有所需的一切或者我知道如何测试。我确定qs.get()会返回一个同步查询。而且我确定db不是null。正如日志所说,当我尝试获取数据库的状态时,问题似乎来自DatabaseUtils。
答案 0 :(得分:1)
根据DatabaseUtils.getStatementType的源代码,该方法中NPE的唯一可能原因是接收的参数sql
为空。查看您的qs
列表的内容,并尝试一些防御性编程,以确保不会将空值作为参数发送:
String sql=qs.get(z);
if (sql!=null)
{
db.execSQL(sql);
}