0

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.

我的日志猫:

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 对象“数据”——反过来,它应该自动实例化数据库对象。所以这让我很困惑。任何帮助是极大的赞赏。

4

2 回答 2

0

setupDb()您正在调用的方法中Log.e(TAG, ex.getMessage());- 但在这种情况下ex.getMessage()返回null。我不知道你的例外是什么,所以不能评论为什么,但看看发生了什么的一种方法是做

Log.e(TAG, ex.toString());
于 2013-07-09T15:42:54.163 回答
0

我发现了问题所在。Eclipse 以某种方式从我的清单中删除了对我的CustomApp类的引用(即使我确实添加了它)。我会密切注意以确保不会再次发生这种情况。

于 2013-07-10T08:40:01.890 回答