问题
I know this is a pre-asked question. But still I am not able to find solutions.
I am opening a database with a SQLiteOpenHelper and applying insert and query method in SQLiteDatabase object.
But I am getting NullPointerException in query method --
Here is my openhelper class:
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);
}
}
Here is my utility class in which I am applying methods --
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**
}
}
I execute:
Cursor cup;
String[] cm = {"mov_pos"};
cup = utility.getEntry(cm); **-- null pointer**
回答1:
Since you call a static method from a class that extends Activity
, the onCreate
method of that class is obviously not called, that's why you are getting NPE.
Firstly move all your methods in the utility class to your MovieDbHelper class, and use the context
provided in the constructor.
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;
}
}
for example your getEntry
(put it in the MovieDbHelper
) can be rewritten as:
public Cursor getEntry(String[] columns){
SQLiteDatabase db = this.getReadableDatabase();
return db.query("MOVIE",columns,null,null,null,null,null); **-- nullpointer**
}
EDITED:
usage in your activity:
MovieDbHelper helper = new MovieDbHelper(YourActivityName.this);
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
usage in your fragment:
MovieDbHelper helper = new MovieDbHelper(getActivity().getApplicationContext());
String[] cm = {"mov_pos"};
Cursor c = helper.getEntry(cm);
EDIT 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;
}
来源:https://stackoverflow.com/questions/31314482/getting-null-pointer-exception-in-sqlitedatabase-query-method