在开发 Android 应用程序时,我使用Android SQLiteAssetHelper。问题是,有时在访问数据库时,我会收到对象 NullPointerException
中的对象错误DBQuery
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 类中的“数据库”。