0

I want to fetch .mp3 files from internal storage here is the for loop

public ArrayList<File> findsong(File file){
        ArrayList<File> arrayList = new ArrayList<>();
        File[] files = file.listFiles();
        for (File singleFiles: files){
            if (singleFiles.isDirectory() && !singleFiles.isHidden()){
                arrayList.addAll(findsong(singleFiles));
            }else {
                if (singleFiles.getName().endsWith(".mp3")){
                    arrayList.add(singleFiles);
                }
            }
        }
        return arrayList;
    } 

but I encountered null pointer error

java.lang.NullPointerException: Attempt to get length of null array

here is my full code

Context of what am trying to do : I want to display the list of audio files from internal/external storage and since am new to android development I am stuck here, a help will be great

THANKS IN ADVANCE

ListView myListViewForSongs;
    String[] items;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runtimePermission();
        myListViewForSongs= (ListView) findViewById(R.id.listView);
    }
    public void runtimePermission(){
        Dexter.withContext(this).withPermission(Manifest.permission.READ_EXTERNAL_STORAGE).withListener(new PermissionListener() {
            @Override
            public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                display();
            }

            @Override
            public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {

            }

            @Override
            public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                permissionToken.continuePermissionRequest();
            }
        }).check();
    }
    public ArrayList<File> findsong(File file){
        ArrayList<File> arrayList = new ArrayList<>();
        File[] files = file.listFiles();
        assert files != null;
        for (File singleFiles: files){
            if (singleFiles.isDirectory() && !singleFiles.isHidden()){
                arrayList.addAll(findsong(singleFiles));
            }else {
                if (singleFiles.getName().endsWith(".mp3")){
                    arrayList.add(singleFiles);
                }
            }
        }
        return arrayList;
    }
    void display(){
        final ArrayList<File> mySongs=findsong(new File(Environment.getExternalStorageState()));
        items = new String[mySongs.size()];
        for (int i=0;i<mySongs.size();i++){
            items[i]= mySongs.get(i).getName().replace(".mp3","");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
        myListViewForSongs.setAdapter(adapter);
    }
}
  • Please paste the whole code and the whole output of the error. – Themelis Jun 09 '20 at 17:00
  • Also start with showing how you defined `file`. – blackapps Jun 09 '20 at 17:16
  • 1
    So `files` is null. Check for null before use. Return if the value is null. Dont continue. Display a toast to inform the user. – blackapps Jun 09 '20 at 17:17
  • What do you understand from that error message? See: [java.lang.NullPointerException: Attempt to get length of null array](https://stackoverflow.com/questions/33993284/java-lang-nullpointerexception-attempt-to-get-length-of-null-array). – AMC Jun 09 '20 at 23:21
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AMC Jun 09 '20 at 23:22
  • I added my full code kindly go through it – Mad_Flasher_oo7 Jun 10 '20 at 14:38

2 Answers2

0

you are supposed to use content providers to fetch the songs from the internal storage.

Nitin Tej
  • 353
  • 1
  • 7
0

I have successfully fetched audio files using MediaStore

CODE

    String[] proj = {
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA....

and then set them to an ArrayAdapter<>