我知道这是一个预先问过的问题。但我仍然无法找到解决方案
我正在使用SQLiteOpenHelper打开一个数据库,并在SQLiteDatabase对象中应用插入和查询方法
但我在查询方法中得到NullPointerException -
这是我的openhelper课程:
public class MovieDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
static final String DATABASE_NAME = "movie.db";
public MovieDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create_mov_table = "CREATE TABLE" + "MOVIE" + "(" +
"mov_id" + " INTEGER PRIMARY KEY AUTOINCREMENT," +
"mov_pos" + " BLOB);";
sqLiteDatabase.execSQL(create_mov_table);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
// should be your top priority before modifying this method.
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + "MOVIE");
onCreate(sqLiteDatabase);
}
}
这是我的实用工具类,我在其中应用方法 -
public class utility extends ActionBarActivity {
static Context c ;
// convert from bitmap to
// byte array
static SQLiteDatabase db;
public void onCreate(){
utility.c = getApplicationContext();
MovieDbHelper mhelper = new MovieDbHelper(c);
db = mhelper.getWritableDatabase();
}
public static Context getAppContext() {
return utility.c;
}
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
public static void addEntry( byte[] image) throws SQLiteException {
ContentValues cv = new ContentValues();
cv.put("mov_pos", image);
db.insert( "MOVIE", null, cv );
}
public static Cursor getEntry(String[] columns){
return db.query("MOVIE",columns,null,null,null,null,null); **-- nullpointer**
}
}
我执行:
Cursor cup;
String[] cm = {"mov_pos"};
cup = utility.getEntry(cm); **-- null pointer**
答案 0 :(得分:0)
由于您从扩展Activity
的类中调用静态方法,因此该类的onCreate
方法显然未被调用,这就是您获得NPE的原因。
首先将实用程序类中的所有方法移动到MovieDbHelper类,然后使用构造函数中提供的context
。
public class MovieDbHelper extends SQLiteOpenHelper {
private Context context;
private static final int DATABASE_VERSION = 2;
static final String DATABASE_NAME = "movie.db";
public MovieDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
}
例如,您的getEntry
(将其放在MovieDbHelper
中)可以重写为:
public Cursor getEntry(String[] columns){
SQLiteDatabase db = this.getReadableDatabase();
return db.query("MOVIE",columns,null,null,null,null,null); **-- nullpointer**
}
编辑:
在您的活动中使用:
MovieDbHelper helper = new MovieDbHelper(YourActivityName.this);
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
片段中的用法:
MovieDbHelper helper = new MovieDbHelper(getActivity().getApplicationContext());
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
编辑2:
public addEntry( byte[] image) throws SQLiteException {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("mov_pos", image);
try {
db.insertOrThrow("MOVIE", null, cv );
} catch (SQLiteConstraintException e) {
e.printStackTrace();
return false;
}
return true;
}