问题描述
我知道这是一个pre-问的问题。但我仍是没能找到解决办法。结果
我使用的是SQLiteOpenHelper打开数据库,并在SQLiteDatabase对象将插入和查询方法。结果
但我得到的NullPointerException查询方法 - 结果
这里是我openhelper类:
公共类MovieDbHelper扩展SQLiteOpenHelper {
私有静态最终诠释DATABASE_VERSION = 2; 静态最后弦乐DATABASE_NAME =movie.db;
公共MovieDbHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
@覆盖
公共无效的onCreate(SQLiteDatabase sqLiteDatabase){
字符串create_mov_table =CREATE TABLE+MOVIE+(+
mov_id+INTEGER PRIMARY KEY AUTOINCREMENT,+
mov_pos+BLOB);;
sqLiteDatabase.execSQL(create_mov_table); } @覆盖
公共无效onUpgrade(SQLiteDatabase sqLiteDatabase,INT I,INT I1){
//应该是你修改此方法之前重中之重。
sqLiteDatabase.execSQL(DROP TABLE IF EXISTS+MOVIE); 的onCreate(sqLiteDatabase);
}
}
下面是在我申请我的方法实用类 -
公共类公用事业扩展ActionBarActivity {
静态上下文℃;
//转换为位图
//字节数组
静态SQLiteDatabase分贝;
公共无效的onCreate(){ utility.c = getApplicationContext();
MovieDbHelper mhelper =新MovieDbHelper(C); DB = mhelper.getWritableDatabase(); } 公共静态上下文getAppContext(){
返回utility.c;
}
公共静态的byte []的getBytes(位图位图){
ByteArrayOutputStream流=新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,0,流);
返回stream.toByteArray();
} //从字节数组转换为位图
公共静态位图的getImage(字节[]图像){
返回BitmapFactory.de codeByteArray的(图像,0,image.length);
}
公共静态无效的addEntry(字节[]图像)抛出SQLiteException { ContentValues CV =新ContentValues(); cv.put(mov_pos,图像);
db.insert(电影,空,CV);
}
公共静态光标getEntry(字符串[]列){
返回db.query(电影,列,NULL,NULL,NULL,NULL,NULL); ** - **空指针
}}
我执行:
光标杯;
的String [] =厘米{mov_pos};
杯= utility.getEntry(厘米); ** - 空指针**
既然你打电话从扩展活动
的 A类中的静态方法那类的onCreate
方法显然是不叫,这就是为什么你得到NPE。
首先,在实用类所有的方法移动到MovieDbHelper类,并使用背景
在构造函数中提供。
公共类MovieDbHelper扩展SQLiteOpenHelper {
私人上下文的背景下;
私有静态最终诠释DATABASE_VERSION = 2;
静态最后弦乐DATABASE_NAME =movie.db; 公共MovieDbHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
this.context =背景;
}
}
例如你的 getEntry
(把它放在 MovieDbHelper
)可以被改写为:
公共光标getEntry(字符串[]列){
SQLiteDatabase分贝= this.getReadableDatabase();
返回db.query(电影,列,NULL,NULL,NULL,NULL,NULL); ** - **空指针
}
编辑:
使用您的活动:
MovieDbHelper帮手=新MovieDbHelper(YourActivityName.this);
的String [] =厘米{mov_pos};
光标C = helper.getEntry(厘米);
使用您的片段:
MovieDbHelper帮手=新MovieDbHelper(getActivity()getApplicationContext());
的String [] =厘米{mov_pos};
光标C = helper.getEntry(厘米);
编辑2:
公开的addEntry(字节[]图像)抛出SQLiteException {
SQLiteDatabase分贝= this.getWritableDatabase();
ContentValues CV =新ContentValues(); cv.put(mov_pos,图像); 尝试{
db.insertOrThrow(电影,空,CV);
}赶上(SQLiteConstraintException E){
e.printStackTrace();
返回false;
}
返回true;
}
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**
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;
}
这篇关于获得空指针异常的方法Sqlitedatabase.query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!