-1

Iam working in a desktop application for windows version using java. In my application there is a requirement to search all .php

i use recursive methods;

and REGEX

my code :

import java.io.File;


public class Copier {
public static void find(String source,String rep)
{
    File src=new File(rep);
    if(src.exists() && src.isDirectory())
    {
        String[] tab=src.list();
        for(String s:tab)
        {
            File srcc=new File(rep+"\\"+s);
            if(srcc.isFile())
            {  
                if(srcc.getName().matches(".*"+source+"$"))
                System.out.println(s);
            }

            else
                find(source,srcc.getAbsolutePath());
        }
    }
}

public static void main(String[] args)
{
    find(".php","C:\\");
}
}

But i have this exception :

Exception in thread "main" java.lang.NullPointerException
    at Copier.find(Copier.java:11)
    at Copier.find(Copier.java:21)
    at Copier.main(Copier.java:28)
3
  • 1
    My guess is that src.list() returned a null, but I'm just guessing at the line count. This could happen, eg, because you're not authorized to the directory. Commented Dec 19, 2011 at 12:13
  • yes but i access an a administartor Commented Dec 19, 2011 at 12:31
  • Well, if that line is where the error is coming from, you're getting a file error for some reason. Try testing the location with canRead. Commented Dec 19, 2011 at 12:35

2 Answers 2

1

src.list() returns null. It probably happens because you (current user) does not have access rights to the directory. I guess it is about C:\ (the root directory of disk C). This often happens especially on Windows 7.

First try to debug your code using directory where you have access rights. Then fix your code to care about nulls. Then try to run your program as an administrator.

Sign up to request clarification or add additional context in comments.

1 Comment

i run my application as an administrator but i have this apph2.php creditbatch.php details.php :::::: java.lang.NullPointerException at Copier.find(Copier.java:11) at Copier.find(Copier.java:21) at Copier.main(Copier.java:29) `
1

Change main like below, for debugging purpose.

public static void main(String[] args)
{
    try {
        find(".php","C:\\");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

And add a null check in

if (src != null && src.exists() && src.isDirectory())

Edit:

Below works fine for me, (I am using windows 7).

import java.io.File;

public class Copier {

    public static void find(String source,String rep) {
        File src = new File(rep);
        if (src!= null && src.exists() && src.isDirectory()) {
            String[] tab = src.list();
            if (tab != null) {
                for(String s : tab) {
                    File srcc = new File(rep+"\\"+s);
                    if (srcc.isFile()) {  
                        if (srcc.getName().matches(".*"+source+"$")) {
                            System.out.println(s);
                        }
                    } else {
                        find(source,srcc.getAbsolutePath());
                    }
                }
            } else {
                //System.out.println(" list is null");
            }
        }
    }

    public static void main(String[] args) {
        try {
            find(".java", "C:\\");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2 Comments

i try but i have exception java.lang.NullPointerException at Copier.find(Copier.java:11) at Copier.find(Copier.java:21) at Copier.main(Copier.java:29)
@Anouar code works fine for "c:\\Work\\". I get the same exception only when trying for "c:\". As Alex advised try to check admin rights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.