listFiles 遍历的时候报错 Caused by: java.lang.NullPointerException

public void listFils(File sourceFile){
    File[] files = sourceFile.listFiles();
    for (File file:files){//这里报错
        if (file.isFile()){
            Log.d("kodulf","文件名 "+file.getName());
        }else{
            Log.d("kodulf","目录名 "+file.getName());
            listFils(file);
        }
    }
}


03-21 08:42:41.501 26924-26924/? E/AndroidRuntime: FATAL EXCEPTION: main

03-21 08:42:41.501 26924-26924/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{tech.androidstudio.filereadandwritetwentyexamples/tech.androidstudio.filereadandwritetwentyexamples.MainActivity}: java.lang.NullPointerException
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:507)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:   Caused by: java.lang.NullPointerException
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at tech.androidstudio.filereadandwritetwentyexamples.MainActivity.listFils(MainActivity.java:170)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at tech.androidstudio.filereadandwritetwentyexamples.MainActivity.listFils(MainActivity.java:175)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at tech.androidstudio.filereadandwritetwentyexamples.MainActivity.example6ListFiles(MainActivity.java:164)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at tech.androidstudio.filereadandwritetwentyexamples.MainActivity.onCreate(MainActivity.java:39)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:3683) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:507) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

03-21 08:42:41.501 26924-26924/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 


原因分析:

查看该文件夹的时候该文件夹为空:


解决办法:

添加判断,是否文件列表为null

public void listFils(File sourceFile){
    File[] files = sourceFile.listFiles();
    if(files!=null){
        for (File file:files){
            if (file.isDirectory()){
                Log.d("kodulf", "目录名 " + file.getName());
                listFils(file);
            }else{
                Log.d("kodulf","文件名 "+file.getName());
            }
        }
    }
}

 
  

你可能感兴趣的:(TroubleShooting,开发中遇到的问题解决)