que hace example create clase android nullpointerexception sqliteopenhelper

android - example - que hace la clase sqliteopenhelper



Obtención de excepción de puntero nulo en el método Sqlitedatabase.query (1)

Sé que esta es una pregunta hecha previamente. Pero aún no puedo encontrar soluciones.
Estoy abriendo una base de datos con SQLiteOpenHelper y aplicando el método de inserción y consulta en el objeto SQLiteDatabase.
Pero obtengo NullPointerException en el método de consulta.
Aquí está mi clase de OpenHelper:

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

Aquí está mi clase de utilidad en la que estoy aplicando métodos:

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

Ejecuto:

Cursor cup; String[] cm = {"mov_pos"}; cup = utility.getEntry(cm); **-- null pointer**


Como llamas a un método estático de una clase que amplía la Activity , obviamente no se llama al método onCreate de esa clase, por eso onCreate NPE.

Primero mueva todos sus métodos en la clase de utilidad a su clase MovieDbHelper y use el context provisto en el 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; } }

por ejemplo, getEntry (póngalo en MovieDbHelper ) puede reescribirse como:

public Cursor getEntry(String[] columns){ SQLiteDatabase db = this.getReadableDatabase(); return db.query("MOVIE",columns,null,null,null,null,null); **-- nullpointer** }

EDITADO:

uso en su actividad:

MovieDbHelper helper = new MovieDbHelper(YourActivityName.this); String[] cm = {"mov_pos"}; Cursor c = helper.getEntry(cm);

uso en tu fragmento:

MovieDbHelper helper = new MovieDbHelper(getActivity().getApplicationContext()); String[] cm = {"mov_pos"}; Cursor c = helper.getEntry(cm);

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