尝试对数据库运行某些SQL时,我得到NullPointerException
。
我有一个名为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对象'data' - 反过来,它应该自动实例化数据库对象。所以这让我很难过。非常感谢任何帮助。
答案 0 :(得分:0)
在方法setupDb()
中,您正在调用Log.e(TAG, ex.getMessage());
- 但在这种情况下,ex.getMessage()
会返回null
。我不知道你的例外是什么,所以不能评论为什么,但是一种看待正在发生的事情的方法是
Log.e(TAG, ex.toString());
答案 1 :(得分:0)
我发现了问题所在。 Eclipse中已经以某种方式从我的清单中删除了对我的CustomApp
类的引用(尽管我确实添加了它)。我会密切注意确保不再发生这种情况。