Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

file - Exception java.lang.NullPointerException

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...