Recursive media file algorithm
So I'm trying to program a mediafile scanner that can give me the extension, quality and mediatype(movie,tvshow -> online database search)
of files. Now everything works. I can recursively search through my whole external harddisk without a problem. It retrieves all files and does everything I want it to.
However when I try to scan the c:/ I think I run in to a permission problem because it throws a nullpointer exception while it does receive the files.
This is my recursion alghorithm:
Code :
public void getFiles(File file) { //put files in list
if (file.isDirectory() && (!file.isHidden())) {
File[] folder = file.listFiles();
recurse(folder);
} else {
listOfFiles.add(file);
}
}
public void recurse(File[] file) { //recursive search method
for (File f : file) {
if (f.isDirectory()) {
recurse(f.listFiles());
} else {
listOfFiles.add(f);
}
}
}
Here's what I've tried so far
Code :
public void getFiles(File file) { //put files in list
if (file.isDirectory() && (!file.isHidden()) && file.canRead()
&& file.canWrite() && file.canExecute()) {
File[] folder = file.listFiles();
recurse(folder);
} else {
listOfFiles.add(file);
}
}
public void recurse(File[] file) { //recursive search method
for (File f : file) {
if (f.isDirectory()) {
recurse(f.listFiles());
} else {
listOfFiles.add(f);
}
}
}
Anyone know why it's failing?
full stacktrace
Code :
Exception in thread "main" java.lang.NullPointerException
at VidScan.Model.VideoLibrary.recurse(VideoLibrary.java:37)
at VidScan.Model.VideoLibrary.getFiles(VideoLibrary.java:30)
at VidScan.View.Console.Console.<init>(Console.java:13)
at VidScan.StartApp.main(StartApp.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Re: Recursive media file algorithm
What is the full stack trace? What line doe the error occur on? What exactly is it trying to access?
Re: Recursive media file algorithm
I'm trying to read every file (except hidden ones) off my C drive compare the file to the media formats such as: avi, mkv, etc... Then put them in a list
Problem is in the for loop in recurse according to the stack trace
Re: Recursive media file algorithm
You didn't really answer my questions. What is the stack trace? What line does the error occur on? What exactly is it trying to access?
Re: Recursive media file algorithm
The stacktrace is in my post and I'm trying to read each file off my C drive and determine if it's a videofile or not?
Line 37 in my code
That
Re: Recursive media file algorithm
And what is being accessed on that line? What is its value?
Recommended reading: http://www.javaprogrammingforums.com...t-println.html
Re: Recursive media file algorithm
Code :
....
JmCR.sys
JmCrIcon.dll
jmcr_sd1.inf
jmcr.cat
JmCR.sys
JmCrIcon.dll
jmcr_sd1.inf
SdJmPCIE.dll
Setup.exe
setup.ini
silentsetup.bat
Version.txt
Exception in thread "main" java.lang.NullPointerException
at VidScan.Model.VideoLibrary.recurse(VideoLibrary.java:38)
at VidScan.Model.VideoLibrary.getFiles(VideoLibrary.java:30)
at VidScan.View.Console.Console.<init>(Console.java:13)
at VidScan.StartApp.main(StartApp.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Re: Recursive media file algorithm
Okay, so what does that mean? What is null?
Also, you should check out the File API to understand what can be returned from the functions you're using: Java Platform SE 6
Re: Recursive media file algorithm
Well what you said didn't help directly, but it did give me the answer xD when recursing files you need try catch for unexpected errors. Such a rookie mistake #FML Thanks!!
Re: Recursive media file algorithm
Quote:
Originally Posted by
mobinni
Well what you said didn't help directly, but it did give me the answer xD when recursing files you need try catch for unexpected errors. Such a rookie mistake #FML Thanks!!
That's probably not what you should be doing. Instead, you should attempt to understand why those errors are occurring, as it probably indicates a problem with your logic.
Re: Recursive media file algorithm
Well it's more likely a problem with authentication. You see when going through my entire C:/ folder I'm also accessing files with certain priviliges it could be possible that my recursion algorithm stumbles on such files. I could alter my manifest file granting administrator privileges to test my theorie... Because the logic is simple: Take a file -> check if it's a folder. Yes? Test all the files in that folder and add files to list and so on. No? Add file to collection. I think it's probably authentication because on an external harddisk it can scan the whole thing without a problem and there are enough files on it.
Re: Recursive media file algorithm
Seems to me it would be easier just to see if your program throws the same error when you tell it to specifically look at one of those files. I really doubt that would be the problem. I'm not on your system so I can't do your debugging for you, which is why I tried walking you through the process.
Re: Recursive media file algorithm
I read the material you sent and will take it under advisement. Maybe try it out tomorrow. Thanks though! Don't want you to think I'm ungrateful