mardi 27 janvier 2015

Android 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.



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






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


Logcat ↓



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


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


Aucun commentaire:

Enregistrer un commentaire