Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

android - Getting Null Pointer Exception in Sqlitedatabase.query method

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**
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...