android - Attempt to invoke virtual method on a null object reference error -
i'm trying create song object insert object based array list (for music player app). song object should contain properties album, artist, title, etc.
however, app crashes when run program, , error in log cat.
java.lang.nullpointerexception: attempt invoke virtual method 'boolean java.util.arraylist.add(java.lang.object)' on null object reference
here main class:
private listview listview1; // list of song objects private arraylist<song> songobectlist; // song object passed songobjectlist private song songobject = new song(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // add song objects songobjectlist getmusic(); // initialize adapter songadapter adapter = new songadapter(this, r.layout.listview_item_row, songobectlist); // initialize listview listview1 = (listview)findviewbyid(r.id.listview1); // initialize header view header = (view)getlayoutinflater().inflate(r.layout.listview_header_row, null); // add header listview listview1.addheaderview(header); // add adapter listview listview1.setadapter(adapter); private void getmusic() { // set uri uri contenturi = mediastore.audio.media.external_content_uri; // set projection parameter getcontentresolver query string[] projection = { mediastore.audio.media.album, mediastore.audio.media.artist, mediastore.audio.media.title, mediastore.audio.media.data, mediastore.audio.media.duration }; // run getcontentresolver query final cursor mcursor = getcontentresolver().query( contenturi, projection, null, null, null ); // set song object properties if (mcursor.movetofirst()) { { songobject.album = mcursor.getstring(0); songobject.artist = mcursor.getstring(1); songobject.title = mcursor.getstring(2); songobject.data = mcursor.getstring(3); songobject.duration = mcursor.getstring(4); songobectlist.add(songobject); } while (mcursor.movetonext()); } mcursor.close(); return; }
i don't see initialize songobectlist.
try replacing
// list of song objects private arraylist<song> songobectlist;
with
// list of song objects private arraylist<song> songobectlist = new arraylist<song>();
Comments
Post a Comment