I have found a piece of code for Android(Java) which searches sd-card for a given file name(string) in case that the file exists in sd-card it works fine but if there's no file with that name it will throw null pointer exception. Can anyone help me please? or give me another alternative? here's the code:
public File findFile(File dir, String name) {
File[] children = dir.listFiles();
for(File child : children) {//the exception is thrown here!
if(child.isDirectory()) {
File found = findFile(child, name);
if(found != null) return found;
} else {
if(name.equals(child.getName())) return child;
}
}
return null;
}
Here's the logcat results:
threadid=1: thread exiting with uncaught exception (group=0x400205a0)
FATAL EXCEPTION: main
java.lang.NullPointerException
at ir.zinutech.ssn.Settings.findFile(Settings.java:93)//>> which is "for(File child : children) { "
at ir.zinutech.ssn.Settings.findFile(Settings.java:95)//>> which is "File found = findFile(child, name);//the exception is thrown here!"
at ir.zinutech.ssn.Settings.onClick(Settings.java:69)
at android.view.View.performClick(View.java:2532)
at android.view.View$PerformClick.run(View.java:9293)`end`