mardi 27 janvier 2015

android / JAVA - Sqlite insert NullPointerException error

I'm getting a NullPointerException when try to insert data.


Hope someone can help me...


Here's my code.


It doesn't work. Please tell where I'm going wrong.


DB Insert



dbOpenHelper = new DBOpenHelper(getApplicationContext());


dbOpenHelper.open();

String[] proj = { BaseColumns._ID, MediaStore.Audio.Media.DATA };

musicCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
int idColumn = musicCursor.getColumnIndex(BaseColumns._ID);
int dataColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);

do {
String mid = musicCursor.getString(idColumn);
String mdatastream = musicCursor.getString(dataColumn);
if (mdatastream.endsWith(".mp3")) {
musicIdDb = Integer.parseInt(mid);
//error
mDbOpenHelper.insertData(musicIdDb);
genreList.add(mdatastream);
dbList.add(mid);
}
} while (musicCursor.moveToNext());


Insert Method



public void insertData(int music_id){
String sql = "insert into "+Databases.CreateMusicTable.M_TABLENAME
+" (music_id) values("+music_id+");";
mDB.execSQL(sql);
}


Log:



01-28 11:10:20.630: E/AndroidRuntime(10021): java.lang.RuntimeException: Unable to start service com.hh.dbservice.DBService@42e745f0 with Intent { cmp=com.hh.gplayer/com.hh.dbservice.DBService }: java.lang.NullPointerException


I tried



public long insertDataId(int music_id){
mDB = mDBHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("music_id", music_id);
return mDB.insert(Databases.CreateMusicTable.M_TABLENAME, null, values);
}


but... NullPointerException too. Thanks.


Aucun commentaire:

Enregistrer un commentaire