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)