當我試圖對數據庫運行一些SQL時,我得到一個NullPointerException
。NullPointerException當試圖執行SQL
我有一個類叫CustomApp
,其中extends Application
。這個類創建一個數據幫助類,它控制數據庫的創建和打開。這個數據幫助類的實例被稱爲data
- 這個類中的數據庫被稱爲db
。
然後我有我自己的類,在那裏我存儲靜態的數據庫功能,方便接入 - 讓我只用一個單一的代碼行執行標準數據庫的東西。這是一個名爲Db
的類,其中嵌入了一個名爲Functions
的靜態類。因此,如果我想舉個例子,插入一個聯繫人,我只需調用Db.Functions.insertContact()
函數。我遇到的問題是一個叫做'setupDb()`的函數,如果這些表尚不存在,它就會運行表創建字符串。
我CustomApp類:
public class CustomApp extends Application
{
public static CustomDataHelper data;
public static CustomApp app;
@Override
public void onCreate()
{
super.onCreate();
app = this;
data = new CustomDataHelper();
}
}
我CustomDataHelper類:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CustomDataHelper {
private static final int DATABASE_VERSION = 1;
public SQLiteDatabase db = null;
public CustomDataHelper()
{
SQLiteOpenHelper o = new MyOpenHelper(CustomApp.app, "MyData.db");
this.db = o.getWritableDatabase();
}
// Rest of standard DataHelper class
private class MyOpenHelper extends SQLiteOpenHelper
{
MyOpenHelper(Context context,String DatabaseName)
{
super(context, DatabaseName, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Standard data code here
}
}
}
相關代碼Db
類:
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Db
{
public static Contact contact = null;
static final String TAG = "DbFunctions";
public static class Functions
{
static final String TABLE_CONTACTS = "Contact";
static final String CONTACTS_ID = "id";
static final String CONTACTS_FNAME = "firstName";
static final String CONTACTS_LNAME = "lastName";
static final String CONTACTS_MOBNUM = "mobileNumber";
static final String CONTACTS_SQL = "create table if not exists " + TABLE_CONTACTS +
"(" + CONTACTS_ID + " text not null, " + CONTACTS_FNAME + " text null, " + CONTACTS_LNAME + " text null, " +
CONTACTS_MOBNUM + " text null)";
static final String ALL_SQL = CONTACTS_SQL;
public static boolean setupDb()
{
try
{
CustomApp.data.db.execSQL(ALL_SQL);
return true;
}
catch (Exception ex)
{
Log.e(TAG, ex.getMessage());
return false;
}
}
}
}
當我調用的代碼行:Db.Functions.setupDb();
,我得到NullPointerException
。
我的logcat:
07-09 16:24:26.596: E/AndroidRuntime(18660): FATAL EXCEPTION: main
07-09 16:24:26.596: E/AndroidRuntime(18660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproj/com.example.testproj.MainActivity}: java.lang.NullPointerException: println needs a message
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.os.Looper.loop(Looper.java:137)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread.main(ActivityThread.java:4921)
07-09 16:24:26.596: E/AndroidRuntime(18660): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 16:24:26.596: E/AndroidRuntime(18660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-09 16:24:26.596: E/AndroidRuntime(18660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-09 16:24:26.596: E/AndroidRuntime(18660): at dalvik.system.NativeStart.main(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660): Caused by: java.lang.NullPointerException: println needs a message
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.util.Log.println_native(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.util.Log.e(Log.java:297)
07-09 16:24:26.596: E/AndroidRuntime(18660): at com.example.testproj.Db$Functions.setupDb(Db.java:55)
07-09 16:24:26.596: E/AndroidRuntime(18660): at com.example.testproj.MainActivity.onCreate(MainActivity.java:16)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.Activity.performCreate(Activity.java:5206)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-09 16:24:26.596: E/AndroidRuntime(18660): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-09 16:24:26.596: E/AndroidRuntime(18660): ... 11 more
所以我用的斷點追查這行代碼是造成問題的,其CustomApp.data.db.execSQL(ALL_SQL);
。現在我發現這是因爲data
對象實際上是空的 - 但爲什麼呢?當創建CustomApp
,它應該已經自動實例化對象datahelper「數據」 - 這,反過來,應該有自動實例化的數據庫對象。所以這讓我非常難過。任何幫助是極大的讚賞。
發表您的logcat –
@AleksG完整的堆棧跟蹤完成:) –
你已經註冊您的自定義應用程序類的'Manifest.xml'? – nif