java - How to access from "onCreate()" an Arraylist variable in another method of same class? -


i want access arraylist variable (mysongs) declared globally , used in oncreate() method in method called btnnext(). value of variable taken class using intent.

but i'm not able value of variable in btnnext() method.

intent = getintent(); bundle b = i.getextras(); mysongs = (arraylist) b.getparcelablearraylist("songlist"); position = b.getint("pos",0); 

it's giving null pointer exception on variable. when checked in log, it's showing 'null' in method whereas when checked on log outside btnnext() method, works fine.

public void btnnext(){     log.d("data", "songs"+mysongs);      if(isrepeat==true || isshuffle==true)     {         isrepeat = false;         btnrepeat.setimageresource(r.drawable.btn_repeat_default);          isshuffle = false;         btnshuffle.setimageresource(r.drawable.btn_shuffle_default);     }     mp.stop();     mp.release();     position = (position+1) % mysongs.size();     log.d("data", "p"+position);     u = uri.parse(mysongs.get(position).tostring());     mp = mediaplayer.create(getapplicationcontext(),u);     displaysongname(position);     mp.start();     btnplay.setimageresource(r.drawable.btn_pause);     //toast.maketext(this, "method called", toast.length_short).show();     log.d("data", "p");     } 

what problem?

replace

mysongs = (arraylist) b.getparcelablearraylist("songlist");  

with

mysongs = new arraylist<object>(b.getparcelablearraylist("songlist")); 

Comments