I am writing a program for the Android operating system. It is required to put list [i] in a separate array, but when you start the emulator, it throws an error ... what could be the problem?

private void search(File aDirectory, List res, int objectType) { File[]list = aDirectory.listFiles(); for (int i = 0; i < list.length; i++) { if (list[i].isDirectory()) { if (objectType != FILES && accept(list[i].getName())) { directoriesNumber++; } search(list[i], res, objectType); } else { if (objectType != DIRECTORIES && accept(list[i].getName())) { filesNumber++; totalLength += list[i].length(); newlist[z] = list[i]; // запись в новый массив z++; } } } 

04-21 12:03:49.930: D/AndroidRuntime(1330): Shutting down VM 04-21 12:03:49.930: W/dalvikvm(1330): threadid=1: thread exiting with uncaught exception (group=0x40015560) 04-21 12:03:49.960: E/AndroidRuntime(1330): FATAL EXCEPTION: main 04-21 12:03:49.960: E/AndroidRuntime(1330): java.lang.RuntimeException: Unable to start activity ComponentInfo{jj.kk/jj.kk.HhhActivity}: java.lang.NullPointerException 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:99) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:123) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-21 12:03:49.960: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method) 04-21 12:03:49.960: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:507) 04-21 12:03:49.960: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-21 12:03:49.960: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-21 12:03:49.960: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method) 04-21 12:03:49.960: E/AndroidRuntime(1330): Caused by: java.lang.NullPointerException 04-21 12:03:49.960: E/AndroidRuntime(1330): at jj.kk.HhhActivity.search(HhhActivity.java:90) 04-21 12:03:49.960: E/AndroidRuntime(1330): at jj.kk.HhhActivity.browseTo(HhhActivity.java:104) 04-21 12:03:49.960: E/AndroidRuntime(1330): at jj.kk.HhhActivity.onCreate(HhhActivity.java:47) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-21 12:03:49.960: E/AndroidRuntime(1330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 04-21 12:03:49.960: E/AndroidRuntime(1330): ... 11 more

  • What kind of mistake is that? - VioLet
  • Sorry! The application xXx has stoppeded unexpectedly.Please try again .... - BUG
  • The problem appears after I try to write everything into a new array ... - BUG
  • not informative - Gorets
  • It is necessary to view the number of files in the directory, as well as in all the directories attached to it, the list [i] records all found files. if(list[i].isDirectory()) - here if a directory is found, then the search starts in a subdirectory. search(list[i], res, objectType); that is, search is called again. If a file is found, then else works. And write to the array where all my files will be stored. newlist[z]=list[i]; z is a global variable. private void search(File aDirectory,List res ,int objectType) is called itself several times, so there is nonsense in list[i] . - BUG

2 answers 2

I see the line "Caused by: java.lang.NullPointerException". In Java, there is no leg in the tooth, but something tells me that you have a null pointer — it goes out of the array, for example (check z — it can be larger than the size of the newlist or something).

    Where is the test for going beyond the array?

    java.lang.NullPointerException - this means that you have not initialized a variable, I advise you to debug in this place.