Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework netbeans
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
NullPointerException
shown as I involve that comment statement in code.
 

 
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 19-Sep-12 4:12am
Edited 19-Sep-12 5:02am
TorstenH.33.3K
v2
Comments
TorstenH. at 19-Sep-12 11:03am
   
how is your homework going so far?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

    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:\\");
    }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Non valid answer removed by me.
  Permalink  
v2
Comments
Akkywadhwa at 19-Sep-12 10:23am
   
Richard getFolders is a static function here which can be called by their class name....
Richard MacCutchan at 19-Sep-12 10:25am
   
My mistake I am just trying this code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try changing your code to test for null values of b thus:
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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 766
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100