My aim is to get list of all mp3 files in my computer(below code in c: directory). But when I run this code, I am getting NullPointerException
. But works well for other directory like(e:).
public class music {
public static void main(String args[]){
extract("c:\\");
}
public static void extract(String p){
File f=new File(p);
File l[]=f.listFiles();
for(File x:l)
{
//System.out.println(x.getName());
if(x.isHidden()||!x.canRead())
continue;
if(x.isDirectory())
extract(x.getPath());
else if(x.getName().endsWith(".mp3"))
System.out.println(x.getPath()+"\\"+x.getName());
}
}
}
In Windows operating system. C Drive (Windows drive) have system file that used by windows while running and some file that locked by windows. When your code try to access that files its through exception.
Try to run this code with other then C:// drive..
Add Try catch or null check for this files:
import java.io.*;
public class Music {
public static void main(String args[]){
extract("c:\\");
}
public static void extract(String p){
File f=new File(p);
File l[]=f.listFiles();
for(File x:l){
if(x==null) return;
if(x.isHidden()||!x.canRead()) continue;
if(x.isDirectory()) extract(x.getPath());
else if(x.getName().endsWith(".mp3"))
System.out.println(x.getPath()+"\\"+x.getName());
}
}
}