当我尝试打开一个用Null Pointer Exception编写的SQLiteDatabase时,我遇到了崩溃。 以下是我实例化SQLiteOpenHelper的方法:
private final OpenHelper mOpenHelper;
public static DataManager getInstance() {
if (sInstance == null) {
sInstance = new DataManager();
}
return sInstance;
}
private DataManager() {
mOpenHelper = new OpenHelper(MainApp.getInstance(), MainApp.getInstance().getWalletToken());
SQLiteDatabase.loadLibs(MainApp.getInstance());
}
其中MainApp是我的应用程序对象。
这是我正在尝试做的事情:
SQLiteDatabase db = null;
try {
db = mOpenHelper.getWritableDatabase();
for (Card card : cards) {
if (card == null) {
continue;
}
addCard(card, db);
ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_NAME_NICKNAME, card.getNickname());
File imageFile = getImageFile(Integer.toString(card.getIndex()));
if (ImageUtils.saveImage(card.getCardImage(), imageFile)) {
values.put(DataEntry.COLUMN_NAME_IMAGE_FILE, imageFile.getAbsolutePath());
}
String whereClause = DataEntry.COLUMN_NAME_CARD_INDEX + " = "
+ card.getIndex();
db.update(DataEntry.TABLE_NAME, values, whereClause, null);
}
} finally {
if (db != null) {
db.close();
}
}
这是崩溃输出:
03-10 16:49:10.533: E/AndroidRuntime(8771): FATAL EXCEPTION: main
03-10 16:49:10.533: E/AndroidRuntime(8771): java.lang.NullPointerException
03-10 16:49:10.533: E/AndroidRuntime(8771): at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1957)
03-10 16:49:10.533: E/AndroidRuntime(8771): at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
03-10 16:49:10.533: E/AndroidRuntime(8771): at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)
03-10 16:49:10.533: E/AndroidRuntime(8771): at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
03-10 16:49:10.533: E/AndroidRuntime(8771): at com.mywebsite.DataManager$OpenHelper.getWritableDatabase(DataManager.java:282)
我用google搜索源代码,但该特定行不包含任何应该抛出异常的内容。我做错了什么?
答案 0 :(得分:1)
我会在loadLibs()
之前致电new OpenHelper()
。
除此之外,我的猜测是getWalletToken()
正在返回null
。 MainApp.getInstance()
未返回null
,否则您将在getWalletToken()
电话本身崩溃。用户如何提供此“钱包令牌”?