Results 1 to 11 of 11
  1. #1
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Smile Getting error when using recrusive method

    Problem is simple i am using my custom made code to loop through every windows folder and file

    Run it under eclipse IDE and get exception, could be this due to the out of memory ?
    Exception in thread "main" java.lang.NullPointerException
    W29NCPA.DLL length 458752
    WLDMLRES.DLL length 122880
    PkgInfo.ini length 2008
    at SystemScanner.scanFilesAndDirectories(SystemScanne r.java:12)
    at SystemScanner.scanFilesAndDirectories(SystemScanne r.java:16)
    at SystemScanner.main(SystemScanner.java:7)

    Java Code:
    import java.io.File;
    
    
    public class SystemScanner {
    
    	public static void main(String[] args) {
    		scanFilesAndDirectories(new File("c:\\"));
    	}
    
    	public static void scanFilesAndDirectories(File path) {
    		if (path.isDirectory()) {
    			for (File f : path.listFiles()) {
    				if (f.isFile()) {
    					System.out.println(f.getName() + " length " + f.length());
    				} else {
    					scanFilesAndDirectories(f);
    				}
    			}
    		}
    	}
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you read the Java doc about this? listFiles() return null, if the path name does not represent a directory.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    As Eranga noted, if 'path' is not a directory at the instant that .listFiles is called, listFiles will return null. How can that happen when you check to see if it's a directory first!? Well, if the directory was deleted/moved/renamed between the time of isDirectory and the time you call listFiles, it could happen.

    There is no real way to lock the file system down, so you have to carefully think through these dynamics first. It turns out, 'isDirectory' is superfluous in this application. Do something like this instead:

    Java Code:
    File [] files = path.listFiles();
    
    if(files != null) {
      for(File f : files) {
    ...
      }
    }
    Inside the ellipses, make no assumptions about the continued existence of any file, and read teh API very carefully for any other possible errors/exceptions.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by toadaly View Post
    read teh API very carefully for any other possible errors/exceptions.
    Actually this is must. Lots of members are here suggest this too many. Once you read the API, take care to handle all exceptions explain there.

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    scanFilesAndDirectories(new File("c:\\"));
    is not a directory...it is a root (
    so you always get out the loop )

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    But the prog style is quite strange as for Java itself (
    Usually you have to scan one maybe two folders not a huge range of C drive (
    Maybe that a logic or stragedy error?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    It can be. C drive is the system drive in most of the case. So searching the system files can be a jumble at all.

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I don't see anything wrong with treating the 'c' drive as a directory, regardless of language. The problem is most likely a race condition, since the program depends on a static file system, and the file system is not static.

  10. #10
    jurka is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by toadaly View Post
    I don't see anything wrong with treating the 'c' drive as a directory, regardless of language. The problem is most likely a race condition, since the program depends on a static file system, and the file system is not static.
    I have to agree you, yeah and these debates about is C:\ directory or driver, can be proved in one way, use new File("C:\").isDirectory().

    File system is kinda dynamic, so I explored the NullPointerException and what i found out there is folder with 0bytes, but inside it there is application 500Kb. Here if you want to see the picture ImageShack - Image Hosting :: kaustnulleg2.jpg.

    But i haven't found out how to get the right result of this folder. Here is code
    Java Code:
    import java.io.File;
    import java.util.logging.FileHandler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;
    
    
    public class SystemScanner {
    
    	static long fileLengthSum = 0L;
    	static Logger log;
    	
    	public static void main(String[] args) {
    		log = Logger.getLogger("myLog");
    		
    		try { 
    			FileHandler fileHandler = new FileHandler("c:\\MyErrorLog.log", true);
    			log.addHandler(fileHandler);
    			fileHandler.setFormatter(new SimpleFormatter());
    			
    			scanFilesAndDirectories(new File("c:\\"));
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void scanFilesAndDirectories(File path) {
    		if (path.isDirectory() && path != null) {
    			for (File f : path.listFiles()) {
    				checkFile(f);
    				if (f.isFile()) {
    					System.out.println(f.getName() + " length " + f.length());
    					fileLengthSum += f.length();
    				} else {
    					scanFilesAndDirectories(f);
    				}
    			}
    		}
    	}
    	
    	public static void checkFile(File f) {
    		if (!f.isFile() && !f.isDirectory() && !f.exists() && !f.canRead()) {
    //			System.err.println("Problems with file" + f.getName());
    			String fileName = f.getName();
    			String message = "";
    			log.log(Level.WARNING, "Problem with file " + fileName);
    			
    			if (!f.isFile()) {
    				message = "Is not a file";
    			} else if (!f.isDirectory()) {
    				message = "Not directory";
    			} else if (!f.exists()) {
    				message = "File is not existing";
    			} else if (!f.canRead()) {
    				message = "File is not readable";
    			}
    			log.warning(message + " " + fileName);
    		}
    	}
    }

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by toadaly View Post
    I don't see anything wrong with treating the 'c' drive as a directory, regardless of language. The problem is most likely a race condition, since the program depends on a static file system, and the file system is not static.
    What I think is, Webuser trying to say that a root is declared as a directory and at the end it cause for a null exception.

    My point is just s tip, not to deal with any language at all. Scanning such a massive drive can be mess to solve problems.

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 09:05 PM
  2. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  3. Error with getString() method in
    By trivektor in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-14-2008, 07:30 PM
  4. Replies: 10
    Last Post: 09-18-2008, 10:41 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •