0

I am new to android studio. my problem is when I run the code the emulator jumps out the application. but when I replace the searchtxtFile() method with a simple setName() method the application runs with no problem. I tested the application on the note4 and it worked properly, but on the emulator the listFiles() return null. I have checked the path. I ran this code on the IntelliJ Idea and it worked but on the Android Studio it has the error: "java.lang.NullPointerException: Attempt to get length of null array"

Thanx in advance.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    List<Integer> fileNos=new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ShowFileDirectory fileList = new ShowFileDirectory();
        fileList.searchtxtFiles("d://sampleb",fileNos);       -->(Line20)
    }

}

ShowFileDirectory.java

import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ShowFileDirectory {

    public String setName(String name){
        return name;
    }

    public void searchtxtFiles(String folderAddress, List<Integer> fileNos2) {

        File directory;
        int fileNo=0;
        directory = new File(folderAddress);
        File[] filesInsideDirectory=directory.listFiles();
        int j=0;

        for(File file : filesInsideDirectory){         -->(Line 73)
            String extension = "";
            int i = file.getName().lastIndexOf('.');
            if (i > 0) {
                extension = file.getName().substring(i+1);
            }
            if (extension.equals("txt")) {
                fileNo= Integer.parseInt(file.getName().substring(0,i));
                fileNos2.add(fileNo);
                j++;
            }
        }
    }

}

the logcat is:

  05-16 20:47:47.990 2942-2942/com.ali.searchfiles I/Process: Sending    signal. PID: 2942 SIG: 9
  05-16 20:47:53.974 4065-4065/com.ali.searchfiles W/System: ClassLoader   referenced unknown path: /data/app/com.ali.searchfiles-1/lib/x86
  05-16 20:47:55.373 4065-4065/com.ali.searchfiles W/System: ClassLoader referenced unknown path: /data/app/com.ali.searchfiles-1/lib/x86
  05-16 20:47:55.461 4065-4065/com.ali.searchfiles W/art: Before Android  4.1, method android.graphics.PorterDuffColorFilter  android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android. graphics.PorterDuffColorFilter, android.content.res.ColorStateList,  android.graphics.PorterDuff$Mode) would have incorrectly overridden the package- private method in android.graphics.drawable.Drawable
  05-16 20:47:55.494 4065-4065/com.ali.searchfiles D/AndroidRuntime:   Shutting down VM


                                                               ---------   beginning of crash
  05-16 20:47:55.494 4065-4065/com.ali.searchfiles E/AndroidRuntime: FATAL EXCEPTION: main
      Process:       com.ali.searchfiles, PID: 4065
         java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.ali.searchfiles/com.ali.searchfiles.MainActivity}:    java.lang.NullPointerException: Attempt to get length of null array
          at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
          at android.app.ActivityThread.-wrap11(ActivityThread.java)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:5417)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
       Caused by: java.lang.NullPointerException: Attempt to get length of null array
          at com.ali.searchfiles.ShowFileDirectory.searchtxtFiles(ShowFileDirectory.java:73)
          at com.ali.searchfiles.MainActivity.onCreate(MainActivity.java:20)
          at android.app.Activity.performCreate(Activity.java:6237)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:5417) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  • What part of "Attempt to get length of null array" is unclear? – ivan_pozdeev May 18 '16 at 10:41
  • I don't know why listFile() returns null on the emulator while working properly on the note4. I have checked the path. it is not empty. – ali khalife soltani May 20 '16 at 02:15
  • Now it's a duplicate of another one - http://stackoverflow.com/questions/33296223/listfiles-returns-null-on-android-6-0-emulator - that is the 1st in Google on "android listfiles null emulator". – ivan_pozdeev May 20 '16 at 07:24

2 Answers2

1

you should specify correct path for Files.

To get Externalstorage path call this function

 Environment.getExternalStorageDirectory();

if you are trying to access Somefile folder present in sd card

give Environment.getExternalStorageDirectory()+"/Somefile" as path

and before listing files always check whether the file exists or not

File f=new File("path");
    if(f.exists()){
        //Do file operation
    }else{
        //File not exist.
    }
Jayanth
  • 5,954
  • 3
  • 21
  • 38
0
public File[] listFiles()

return An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

As you know this may return null , so most probably you are supplying wrong path hence at least provide a check for null filearray and also verify the path.

Alok
  • 881
  • 1
  • 11
  • 18