在Android應用程序的開發中,我使用Android SQLiteAssetHelper。 問題是,偶爾在訪問數據庫中,我得到一個錯誤的對象NullPointerException
的對象DBQuery
NullPointerException與Android SQLiteAssetHelper
public class DBQuery extends MyDatabase {
private static MyDatabase dataBaseHelper;
public DBQuery(Context context) {
super(context);
dataBaseHelper = new MyDatabase(context);
}
public static void closed() {
dataBaseHelper.close();
}
public static int getFDay(int year) {
Cursor cursor = database.rawQuery("SELECT offset FROM china_years WHERE year = ?",
new String[] { Integer.toString(year) });
cursor.moveToFirst();
String yearS;
try {
yearS = cursor.getString(0);
} catch (Exception e) {
yearS = "0";
e.printStackTrace();
}
cursor.close();
return Integer.parseInt(yearS);
}
/* other queries... */
}
MyDatabase的類:
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "db.sqlite";
private static final int DATABASE_VERSION = 2;
public static SQLiteDatabase database;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
database = getReadableDatabase();
}
}
UDP: 對不起,空對象是在「數據庫」 DBQuery類。
你有沒有打開數據庫,然後執行getFDay(int year); –
我打開數據庫與數據庫= getReadableDatabase(); – bloodvlad