Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Recursive media file algorithm

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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:
    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
     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
    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)
    Last edited by mobinni; May 1st, 2012 at 08:18 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
            for (File f : file) {
    That

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursive media file algorithm

    ....
    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)

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!!

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Recursive media file algorithm

    Quote Originally Posted by mobinni View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Simple media type program
    By deepwoodsv in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 31st, 2011, 07:34 AM
  2. Media Player Help in java
    By Tajinder in forum Java SE APIs
    Replies: 2
    Last Post: September 27th, 2011, 12:00 PM
  3. Replies: 5
    Last Post: March 16th, 2011, 08:55 PM
  4. Stream with servlet in windows media
    By migalvcon in forum Java Servlet
    Replies: 0
    Last Post: May 19th, 2010, 05:03 AM

Tags for this Thread