-1

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**
Mayank Jindal
  • 355
  • 5
  • 15

1 Answers1

0

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;
}
Kise
  • 2,823
  • 1
  • 24
  • 43
  • I want to use the same databse for getentry and addentry so is it necessary to declare the database outside the two methods. – Mayank Jindal Jul 09 '15 at 11:05
  • what? you don't seem to understand how `SQLiteOpenHelper` works at all. It's the same database no matter how many times you initialise `MovieDbHelper`. If the db is already created then it will not call `onCreate`, you know? – Kise Jul 09 '15 at 11:12
  • ok but now it is giving null pointer at SQLiteDatabase db = this.getReadableDatabase(); – Mayank Jindal Jul 09 '15 at 11:17
  • Have you put it in `MovieDbHelper`? – Kise Jul 09 '15 at 11:19
  • it's the error log that says NullPointerException. Just copy it and paste into http://pastebin.com/ then link it here. – Kise Jul 09 '15 at 11:22
  • but why do you using your DB in the adapter? It should be in the Activity – Kise Jul 09 '15 at 11:37
  • now it is giving Cannot resolve method 'getActivity()' as I am using this in ImageAdapter which uses Baseadapter – Mayank Jindal Jul 09 '15 at 11:39
  • paste the whole content of your ImageAdapter on pastebin and I will modify it – Kise Jul 09 '15 at 11:42
  • ya now it worked.But I jst want to know how did u do this ?? – Mayank Jindal Jul 09 '15 at 11:59
  • just look at the constructor of the `MovieDbHelper`. It takes `Context` as a parameter. I simply assigned it in your code, where you have the `mContext` ready to use. – Kise Jul 09 '15 at 12:01
  • I am trying to build an app as given in https://docs.google.com/document/d/1gtXUu1nzLGWrGfVCD6tEA0YHoYA9UNyT2yByqjJemp8/pub?embedded=true So as soon as I proceed I am getting another error – Mayank Jindal Jul 09 '15 at 12:07
  • Cursor c = mdb.getEntry(cm); c.moveToFirst(); byte[] image = c.getBlob(position); -- error android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 this is my project on git - https://github.com/gol197/Popular_Movies_14CE10032_GSC.git – Mayank Jindal Jul 09 '15 at 12:11
  • there's no data in your table, add some before getting. – Kise Jul 09 '15 at 12:14
  • I want to get it according logic. – Mayank Jindal Jul 09 '15 at 12:17
  • I have updated my answer with the add method. I can only help you with the syntax, I can't give you the logic. – Kise Jul 09 '15 at 12:18
  • If u have some time please look at the whole project. – Mayank Jindal Jul 09 '15 at 12:18
  • Sorry but I can't. That's all I could help. – Kise Jul 09 '15 at 12:19
  • http://stackoverflow.com/questions/34249254/unable-to-access-changed-value-of-variable-from-another-class-in-android-studio Have a look at this question – Mayank Jindal Dec 13 '15 at 08:26