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**