在我的应用程序中,我正在从SD卡中检索文件,检查后是.jpg oe .png图像我在文件数组中存储文件的路径。但我得到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)
答案 0 :(得分: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;