0

DisplayActivity.java

I'm relatively new to android.I am trying to make a list view that contains music.The app is perfectly build but then when i try to run it on the device it crashes.I try to debug the app and it gave me the above error.Here are my following code

package com.bytetex.azmusic;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;

import java.io.File;
import java.util.ArrayList;

public class DisplayActivity extends AppCompatActivity {

    private String[] itemsAll;
    private ListView mSongsLists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        appExternalStorageStoragePermission();
        mSongsLists=findViewById(R.id.songLists);
    }

    public void appExternalStorageStoragePermission() {
        Dexter.withContext(this)
                .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        dispalyAudioSongName();

                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {

                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        token.continuePermissionRequest();

                    }
                }).check();
    }

    public ArrayList<File> readOnluAudioSongs(File file) {
        ArrayList<File> arrayList = new ArrayList<>();
        File[] allFiles = file.listFiles();
        assert allFiles != null;
        for (File individualFile : allFiles) {
            if (individualFile.isDirectory() && !individualFile.isHidden()) {
                arrayList.addAll(readOnluAudioSongs(individualFile));
            } else {
                if (individualFile.getName().endsWith(".mp3") || individualFile.getName().endsWith(".aac") || individualFile.getName().endsWith(".wav") || individualFile.getName().endsWith(".wma")) {
                    {
                        arrayList.add(individualFile);
                    }
                }
            }

        }
        return arrayList;
    }
    private void dispalyAudioSongName()
    {
        final ArrayList<File> audioSong= readOnluAudioSongs(Environment.getExternalStorageDirectory());
        itemsAll=new String[audioSong.size()];
        for (int songCounter=0; songCounter<audioSong.size(); songCounter++)
        {
            itemsAll[songCounter]=audioSong.get(songCounter).getName();
        }
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(DisplayActivity.this, android.R.layout.simple_list_item_1, itemsAll);
        mSongsLists.setAdapter(arrayAdapter);
    }
}

activity_display.xml

here is my xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DisplayActivity">

    <ListView
        android:id="@+id/songLists"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</ListView>


</RelativeLayout>


1 Answers1

0

The mSongsLists variable is declared but never intiliazed with a value before invoking the setAdapter() method on "mSongsLists" in the dispalyAudioSongName() method.

In the onCreate() Method you are invoking appExternalStorageStoragePermission() which internally invokes dispalyAudioSongName() and at this point the mSongsLists is null.

    private ListView mSongsLists; // declaration of mSongsLists with defaultvalue for an object( null in java)        

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_display);
     appExternalStorageStoragePermission(); // dispalyAudioSongName() invoked internally which tries to set adapter on mSongsLists causing nullpointer exception
     mSongsLists=findViewById(R.id.songLists); //mSongsLists variable is assigned a value at this point and prior to this it is null
    }

    private void dispalyAudioSongName(){
     final ArrayList<File> audioSong=  readOnluAudioSongs(Environment.getExternalStorageDirectory());
     itemsAll=new String[audioSong.size()];
     for (int songCounter=0; songCounter<audioSong.size(); songCounter++)
     {
        itemsAll[songCounter]=audioSong.get(songCounter).getName();
     }
     ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(DisplayActivity.this, android.R.layout.simple_list_item_1, itemsAll);
     mSongsLists.setAdapter(arrayAdapter); // <---nullpointer exception
    }

To fix this you can set the "mSongsLists" first and then invoke appExternalStorageStoragePermission() method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_display);
     mSongsLists=findViewById(R.id.songLists); //set the value first
     appExternalStorageStoragePermission(); // invoke the method second
    }
glegshot
  • 74
  • 7