简体   繁体   English

异常java.lang.NullPointerException

[英]Exception java.lang.NullPointerException

Iam working in a desktop application for windows version using java. Iam使用Java在Windows版本的桌面应用程序中工作。 In my application there is a requirement to search all .php 在我的应用程序中,需要搜索所有.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)

Change main like below, for debugging purpose. 如下更改main,以用于调试目的。

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). 下面对我来说很好用,(我正在使用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();
        }
    }
}

src.list() returns null. src.list()返回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). 我猜这是关于C:\\ (磁盘C的根目录)的。 This often happens especially on Windows 7. 特别是在Windows 7上,这种情况经常发生。

First try to debug your code using directory where you have access rights. 首先尝试使用您具有访问权限的目录调试代码。 Then fix your code to care about nulls. 然后修复您的代码以关心null。 Then try to run your program as an administrator. 然后尝试以管理员身份运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM