I don't have much expirience in Java programming, and I tried to write simple program that iterates through some directory and its subfolders and counts txt files. I have used this code:
import java.io.*;
public class Fajlovi {
public static void main(String[] args) throws Exception {
Fajlovi n=new Fajlovi();
File f=new File("D:\\");
System.out.println("Number of txt files is"+n.listaj(f));
}
public int listaj(File f){
int count=0;
File[] s= f.listFiles();
for(int i=0;i<s.length;i++){
if(s[i].isDirectory())
count+=listaj(s[i]);
else if(s[i].getName().endsWith(".txt")){
count++;
}
}
return count;
}
}
Problem is that it works sometimes depending which directory I specify, but often it throws NullPointerException
and stops execution. I have used some commands to follow execution steps, and I found out that it stops when count+=listaj(s[i])
is called for some directory that I even can't find on my computer and its not hidden.