Click here to Skip to main content
15,887,421 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I have written a code for retrieving contents of a windows partition "D:\" using recursion in java. Can anybody help me that why this from is not running. There is a
Java
NullPointerException
shown as I involve that comment statement in code.



Java
import java.io.*;
class u
{

static public void getFolders(String drive)
{
	try
	{
File a=new File(drive);
File b[]=a.listFiles();
	for(int i=1;i<b.length;i++)
	{
	if(b[i].isDirectory()==true)
	{
String str=b[i].getName().toString();
System.out.println(str);
//getFolders(str);
	}	
	}
	}
	catch(Exception er)
	{
	System.out.println(er);
	}
}


public static void main(String args[])
{
u.getFolders("d:");
}
}


Thanks,
Akky
Posted
Updated 19-Sep-12 5:02am
v2
Comments
TorstenH. 19-Sep-12 11:03am    
how is your homework going so far?

Ok, there are 3 problems here and then 1 thing you probably want to add as Richard MacCutchan said (Checking if b is null)

1 - You need to call getFolders() using d:\\ not d:
2 - Your iterative loop will need to start from the 0 index not 1
3 - Use absolute pathing if you want recursion to work

Here is the code which should work for you.

PS: The null pointer exception is being thrown because you were calling the "getFolders" method by not using the entire path of the directory, but rather the name of the directory. So instead of 'd:\DirectoryName' you were passing in 'Directoryname', which would return null if you call "listFiles()" on it

Java
static public void getFolders(String drive) {
    try {

        File a = new File(drive);
        File b[] = a.listFiles();
        /// Start your loop from index 0 not 1
        for (int i = 0; i < b.length; i++) {
            if (b[i].isDirectory() == true) {
                /// Use absolute pathing to enable "recursiveness"
                String str = b[i].getAbsolutePath().toString();
                System.out.println(str);
                getFolders(str);
            }
        }
    } catch (Exception er) {
        System.out.println(er);
    }
}

public static void main(String[] args) {
    // Call the method with a slash in the name
    getFolders("d:\\");
}
 
Share this answer
 
v2
Non valid answer removed by me.
 
Share this answer
 
v2
Comments
Akkywadhwa 19-Sep-12 10:23am    
Richard getFolders is a static function here which can be called by their class name....
Richard MacCutchan 19-Sep-12 10:25am    
My mistake I am just trying this code.
Try changing your code to test for null values of b thus:
Java
File b[]=a.listFiles();
if (b == null)
{
	System.out.println("b is null"); 
	return;
}

You will also note that your paths (in phase 1) do not include the drive letter prefix so when you recurse into a directory you try a search on an invalid path name which returns no hits.
 
Share this answer
 

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