title: Attempt to get length of null array error, How to solve?
body: <p>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"</p>
  
  <p>Thanx in advance.</p>
  
  <p>MainActivity.java </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      List&lt;Integer&gt; fileNos=new ArrayList&lt;Integer&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ShowFileDirectory fileList = new ShowFileDirectory();
          fileList.searchtxtFiles("d://sampleb",fileNos);       --&gt;(Line20)
      }
  
  }
  </code></pre>
  
  <p>ShowFileDirectory.java</p>
  
  <pre><code>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&lt;Integer&gt; fileNos2) {
  
          File directory;
          int fileNo=0;
          directory = new File(folderAddress);
          File[] filesInsideDirectory=directory.listFiles();
          int j=0;
  
          for(File file : filesInsideDirectory){         --&gt;(Line 73)
              String extension = "";
              int i = file.getName().lastIndexOf('.');
              if (i &gt; 0) {
                  extension = file.getName().substring(i+1);
              }
              if (extension.equals("txt")) {
                  fileNo= Integer.parseInt(file.getName().substring(0,i));
                  fileNos2.add(fileNo);
                  j++;
              }
          }
      }
  
  }
  </code></pre>
  
  <p>the logcat is:</p>
  
  <pre><code>  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) 
  </code></pre>