I’m getting a really strange NPE exception. This is the offending code:
private static int countRecurse(File file) {
Preconditions.checkNotNull(file, "file argument");
int entries = 0;
for (File entry : file.listFiles()) { //NPE on this line
if (entry != null) {
if (entry.isDirectory())
entries += countRecurse(entry);
else
entries++;
}
}
return entries;
}
I get an NPE exception on the commented line. This is the stacktrace I got:
java.lang.NullPointerException
at controllers.StudyPreparation.countRecurse(StudyPreparation.java:103)
at controllers.StudyPreparation.readDicomFiles(StudyPreparation.java:95)
at controllers.StudyPreparation.access$100(StudyPreparation.java:30)
at controllers.StudyPreparation$1.onReady(StudyPreparation.java:77)
at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$1.apply(JavaWebSocket.scala:48)
at play.core.j.JavaWebSocket$$anonfun$webSocketWrapper$1$$anonfun$apply$
The code ispart of a Play framework application, but I don’t think this could be part of the problem…
Since I already test for file nullity, it can’t be null. If something wrong happens inside the listFiles() method, I’ll expect to see that on stacktrace.
So what could be the problem?
EDIT:
Problem solved checking if file is a directory… the NPE is throwed because file.listFiles() return null.
And even if not documented on javadocs, it return null because the path does not exists.
I didn’t check that because on my development machine the procedure is working well, the problem arise only in production server, but is obviouslly related to some other part in code…
Thanks all for your help.
From javadoc of file.listFiles()
So check if
file.isDirectory()before usingfile.listFiles().