0

It's different from just null pointer exception because it runs fine on android version less then or equal to 6

Previously I was getting error that the thread is doing to much of work and thus it skipped many frames so i tried to run it on different thread But I am getting this error. Any suggestion is highly appreciated. As I am a beginner and also it runs fine on my device Android version 6.0 But not on device of higher version. Can anyone answer why?

enter code here 

public class Musiclist extends AppCompatActivity {

public static ArrayList<File> mymusics;
RecyclerView recyclerView;
Adpterformymusic adpterformymusic;


@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.musiclistd);

    mymusics=new ArrayList<>();
    recyclerView=findViewById(R.id.recycleview);

    peeMission();


    Runnable runnable=new Runnable() {
        @Override
        public void run() {

         mymusics=findsongs(Environment.getExternalStorageDirectory());
        adpterformymusic=new Adpterformymusic(Musiclist.this,mymusics);

         recyclerView.setLayoutManager(new LinearLayoutManager(Musiclist.this));
            recyclerView.setAdapter(adpterformymusic);
        }


        public ArrayList<File> findsongs(File file)
        {
            ArrayList<File> mosiac=new ArrayList<>();
            File [] files=file.listFiles();
            assert files != null;
            Log.d("checking length of file",String.valueOf(files.length));
            for(File single:files)
            {
                if(single.isDirectory() && !single.isHidden())
                {
                    mosiac.addAll(findsongs(single));
                }
                else
                {
                    if(single.getName().endsWith(".mp3"))
                    {

                        mosiac.add(single);
                    }
                }

            }


            return mosiac;
        }

    };
    Thread thread=new Thread(runnable);
    thread.start();


}

@RequiresApi(api = Build.VERSION_CODES.M)
public void peeMission()
{
    if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
    {


    }
    else
    {
        ActivityCompat.requestPermissions(Musiclist.this
                ,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE},2);
    }


}

}

this is java code to get songs list, In my logcat it show's

Logcat

Logcat 2020-12-13 22:47:33.094 12296-12377/com.example.read_it1 E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: com.example.read_it1, PID: 12296
java.lang.NullPointerException: Attempt to get length of null array
    at com.example.read_it1.Musiclist$1.findsongs(Musiclist.java:60)
    at com.example.read_it1.Musiclist$1.run(Musiclist.java:47)
    at java.lang.Thread.run(Thread.java:919)
  • 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) – a_local_nobody Dec 14 '20 at 07:23
  • android studio is just an IDE, meaning that it helps you to code. please use the `android` tag and not `android-studio`, unless your question actually is regarding the IDE itself – a_local_nobody Dec 14 '20 at 07:24
  • Your arrayList which holds the file is null please check the list it should not be null – Deepak Shukla Dec 14 '20 at 07:38
  • Also put your problem in the post itself. Not only in the subject. Never refer to the subject. – blackapps Dec 14 '20 at 09:40

0 Answers0