0

在我的应用程序中,我从 sdcard 中检索文件,在检查它是 .jpg oe .png 图像后,我将文件路径存储在 File 数组中。但我得到 NullPointerException 也存在文件路径。以下代码有什么问题。请帮忙

 File[] image;
  File f = new File("/sdcard/download");//or Environment.getExternalStorageDirectory().getPath()
        File[] files = f.listFiles();

        int j=0;
        for(int i = 0; i < files.length; i++,j++) {
            File file1 = files[i];

            String myfile = file1.getPath().substring(file1.getPath().lastIndexOf("/")+1,file1.getPath().length()).toLowerCase(); 
            System.out.println("file is : "+files[i]);
            if(myfile.endsWith(".jpeg")||myfile.endsWith(".png")|| myfile.endsWith(".gif") || myfile.endsWith(".jpg"))
            {
                System.out.println("file is : "+files[i]);
                image[j]=files[i];//here i'm getting exception
                }
            }

例外——

  02-15 19:28:36.463: E/AndroidRuntime(12918): FATAL EXCEPTION: main
02-15 19:28:36.463: E/AndroidRuntime(12918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.zproject/com.my.zproject.ImportFile}: java.lang.NullPointerException
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.os.Looper.loop(Looper.java:123)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at java.lang.reflect.Method.invoke(Method.java:521)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at dalvik.system.NativeStart.main(Native Method)
02-15 19:28:36.463: E/AndroidRuntime(12918): Caused by: java.lang.NullPointerException
02-15 19:28:36.463: E/AndroidRuntime(12918):    at com.my.zproject.ImportFile.onCreate(ImportFile.java:76)
02-15 19:28:36.463: E/AndroidRuntime(12918):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
4

1 回答 1

0

你需要image像这样初始化数组

File[] image;
File f = new File("/sdcard/download");//or Environment.getExternalStorageDirectory().getPath()
    File[] files = f.listFiles();
    image = new File[files.length]
    int j=0;
于 2013-02-15T14:14:21.357 回答