-1

I'm a beginner in android and I want to create a simple MP3 player. But i got the following Exception.

NullPointerException - Attempt to get length of null array.

Using adb shell i verified in that path mp3 files are exist. This is my code

Activity Class

public class Display extends ListActivity {
   Button stop;
   private static final String PATH = new String("/sdcard/");
   private List<String> songs = new ArrayList<String>();
   private MediaPlayer mp = new MediaPlayer();
   private ListView mainListView ;
   String name;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.listview);
      stop = (Button) findViewById(R.id.stopbtn);
      mainListView = getListView();
      updatePlayList();
   }

   private void setButtonOnClickListeners(){
       stop.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            mp.stop();
        }
    });}

   @Override
   protected void onListItemClick(ListView list, View view, int position, long id){
    try{
        mp.reset();
        mp.setDataSource(PATH + songs.get(position));
        mp.prepare();
        mp.start();
    }catch(IOException e){
        Log.v("test", e.getMessage());
    }}


    private void updatePlayList() {

        File home = new File(PATH);
        for (File f : home.listFiles()) { // This line causes the exception
            if (f.isFile())
                name = f.getName();
            if(name.contains(".MP3") || name.contains(".mp3")) {
                ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.simplerow, songs);
                mainListView.setAdapter(songList);
            }
        }
}

}

I have added the read permission in manifest file also. And i tried with

Environment.getExternalStorageDirectory()

also. Can anyone help me?

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Konstantin Zadiran Feb 02 '16 at 09:36
  • follow this tutorial : http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ – Kapil Rajput Feb 02 '16 at 10:12

1 Answers1

0

Issue fixed. We should give uses-permission in correct place. It should be a child of manifest and a peer of application