Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I'm trying to run a recursive file search in Java to return a list of all .mp3 files on the system. Now, my background is in .NET, and I'm fairly new to both Linux and Java. My goal is to create a small crossplatform app that is capable of doing this search for me. On the account I need the application to be launched from a website, I figured my best bet was Java. Anyway, I based the code I have now off an article I read online. This is my current code:

Java
startSearch();

List<String> fs = new ArrayList<String>();
public void startSearch()
{

    File[] roots = File.listRoots();

    for(int i = 0; i < roots.length; i++)
    {
        File[] files = new File(roots[i].toString()).listFiles();
        getFiles(files);
    }

    JOptionPane.showMessageDialog(null, fs.get(0)); //This is just to test the function...

}

public void getFiles(File[] files)
{

    for(File file : files) ***
    {
        if(file.isDirectory())
        {
            getFiles(file.listFiles());
        }
        else
        {
            fs.add(file.toString());
        }
    }
}


However, whenever I run it I get the following error on the line above, marked with the ***:

java.lang.NullPointerException
	at Form1.getFiles(Form1.java:132)
	at Form1.getFiles(Form1.java:136)
	at Form1.startSearch(Form1.java:122)
	at Form1.initialize(Form1.java:111)
	at Form1.<init>(Form1.java:56)
	at Form1$1.run(Form1.java:42)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:673)
	at java.awt.EventQueue.access$300(EventQueue.java:96)
	at java.awt.EventQueue$2.run(EventQueue.java:634)
	at java.awt.EventQueue$2.run(EventQueue.java:632)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:643)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)


Any ideas what's wrong? I think it might have something to do with the fact the only drive name returned is '/' and it doesn't know where to look or something. I'm so used to drive based (C:/) filesystems. Any help would be appreciated. Thanks in advance!
Posted
Comments
E.F. Nijboer 10-May-13 7:17am    
Maybe try this console example first. Just to check out what happens. It should result in / on linux.
http://www.tutorialspoint.com/java/io/file_listroots.htm
Richard MacCutchan 10-May-13 7:25am    
Two minutes with your debugger should show you what the content of each variable is, against what it should be.

Java
for(int i = 0; i < roots.length; i++)
{
	File[] files = new File(roots[i].toString()).listFiles();
	if (files != null)
		getFiles(files);
}
 
Share this answer
 
Comments
RadXPictures 10-May-13 10:40am    
I appreciate the help Richard.. unfortunately, adding the != null conditional doesn't seem to correct the problem. When you referred to "two minutes with the debugger".. where in Eclipse may I find that? The only debugger I've been using is the console..
Richard MacCutchan 10-May-13 12:11pm    
1. Adding that if clause did it for me, although I don't have a Linux system to test on.
2. eclipse has a built in debugger; it's on the Run menu item.
Either
Java
for(int i = 0; i < roots.length; i++) {
 File[] files = new File(roots[i].toString()).listFiles();
 if (files != null) {
  getFiles(files);
 }
}

or
Java
public void getFiles(File[] files) {
 if(files != null) {
  for(File file : files) {
   if(file.isDirectory()) {
    getFiles(file.listFiles());
   }
   else {
    fs.add(file.toString());
   }
  }
 }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900