'if'语句中的空指针异常

时间:2014-01-13 21:35:15

标签: java android exception if-statement nullpointerexception

我很生气,因为我的代码仍然出错:

myVar = getFiles();
(...)
public ArrayList<HashMap<String, String>> getFiles(){
    File dir = new File(new String("/sdcard/"));

    if (dir.listFiles(new myFilter()).length > 0) {
        for (File file : dir.listFiles(new myFilter())) {
            (...)
        }
    }
    return myList;
}

at if statement:

if (dir.listFiles(new myFilter()).length > 0) {

这就是我得到的:

01-13 22:28:42.313: W/dalvikvm(13776): threadid=1: thread exiting with uncaught exception (group=0x40c5a1f8)
01-13 22:28:42.318: E/AndroidRuntime(13776): FATAL EXCEPTION: main
01-13 22:28:42.318: E/AndroidRuntime(13776): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.remotedevice/com.example.remotedevice.Player}: java.lang.NullPointerException
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.os.Looper.loop(Looper.java:137)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread.main(ActivityThread.java:4511)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at java.lang.reflect.Method.invokeNative(Native Method)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at java.lang.reflect.Method.invoke(Method.java:511)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at dalvik.system.NativeStart.main(Native Method)
01-13 22:28:42.318: E/AndroidRuntime(13776): Caused by: java.lang.NullPointerException
01-13 22:28:42.318: E/AndroidRuntime(13776):    at com.example.remotedevice.Player.getPlayList(Player.java:120)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at com.example.remotedevice.Player.onCreate(Player.java:56)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.Activity.performCreate(Activity.java:4470)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-13 22:28:42.318: E/AndroidRuntime(13776):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-13 22:28:42.318: E/AndroidRuntime(13776):    ... 11 more

我已经阅读了这个话题:Null Pointer Exception after checking with 'if' statement但是找不到answear,我可以用它来代码:/我尝试过几个'解决方案',但仍然收到这个错误:(请帮助!

2 个答案:

答案 0 :(得分:0)

File listFiles()可能会返回null,例如,如果该文件不是目录。在访问!= null字段之前,您需要检查length以阻止NPE。

现在,为什么/sdcard不是目录?在许多设备上,它是一个符号链接。您必须先使用getCanonicalFile()解析符号链接。

示例:

File[] files = dir.getCanonicalFile().listFiles(new myFilter());
if (files != null) {   
    for (File file : files) {
        //...

此外,存储“昂贵”方法调用的结果(例如listFiles())而不是重复调用它是有益的。

答案 1 :(得分:0)

根据listFiles()的文档,如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。“

因此,您需要检查它是否为null并在这些情况下处理错误。