title: NullPointerException - Attempt to get length of null array (readDirectory())
body: <p>I have the code as shown here. 
  My Problem is a NullPointerException at files.length</p>
  
  <pre><code>for(int i=0; i &lt; files.length; i++){
  </code></pre>
  
  <p>It is caused because i have a "fail readDirectory() errno=13" at</p>
  
  <pre><code>File[] files = f.listFiles();
  </code></pre>
  
  <p>But why do I have a readDirectory fail when the path is good?</p>
  
  <pre><code>package com.example.androidexplorer;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.List;
  
  import android.os.Bundle;
  import android.os.Environment;
  import android.app.AlertDialog;
  import android.app.ListActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  
  public class MainActivity extends ListActivity {
  
      private List&lt;String&gt; item = null;
      private List&lt;String&gt; path = null;
      private String root;
      private TextView myPath;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          myPath = (TextView)findViewById(R.id.path);
  
          root = Environment.getExternalStorageDirectory().getPath();
          getDir(root);
      }
  
      private void getDir(String dirPath)
      {
          myPath.setText("Location: " + dirPath);
          item = new ArrayList&lt;String&gt;();
          path = new ArrayList&lt;String&gt;();
          File f = new File(dirPath);
  
  Log.v("Path: ", dirPath);
  
          Log.v("BEFORE", "Before Reading Fail...");
          File[] files = f.listFiles();
          Log.v("AFTER", "After Reading Fail...");
  
  
          if(!dirPath.equals(root))
          {
              item.add(root);
              path.add(root);
              item.add("../");
              path.add(f.getParent());    
          }
  
          Log.v("CRASH", "1 Line before crash");
          for(int i=0; i &lt; files.length; i++){
              Log.v("AFTER CRASH", "1 Line after crash");
              File file = files[i];
  
              if(!file.isHidden() &amp;&amp; file.canRead()){
                  path.add(file.getPath());
                  if(file.isDirectory()){
                      item.add(file.getName() + "/");
                  }else{
                      item.add(file.getName());
                  }
              }   
          }
  
          ArrayAdapter&lt;String&gt; fileList =
                  new ArrayAdapter&lt;String&gt;(this, R.layout.row, item);
          setListAdapter(fileList);   
      }
  
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
          // TODO Auto-generated method stub
          File file = new File(path.get(position));
  
          if (file.isDirectory())
          {
              if(file.canRead()){
                  getDir(path.get(position));
              }else{
                  new AlertDialog.Builder(this)
                      .setIcon(R.drawable.ic_launcher)
                      .setTitle("[" + file.getName() + "] folder can't be read!")
                      .setPositiveButton("OK", null).show();  
              }   
          }else {
              new AlertDialog.Builder(this)
                      .setIcon(R.drawable.ic_launcher)
                      .setTitle("[" + file.getName() + "]")
                      .setPositiveButton("OK", null).show();
  
            }
      }
  
  }
  </code></pre>
  
  <p>LogCat:</p>
  
  <blockquote>
  <pre><code>&gt; 04-23 15:35:34.084: D/ResourcesManager(20672): creating new
  &gt; AssetManager and set to
  &gt; /data/app/com.example.androidexplorer-1/base.apk 04-23 15:35:34.104:
  &gt; I/art(20672): Created application space
  &gt; /data/dalvik-cache/arm/data@app@com.example.androidexplorer-1@base.apk@classes.dex.art
  &gt; at 0x76eb0000~0x76f15ff8 04-23 15:35:34.104: I/art(20672): Loaded art
  &gt; file:
  &gt; /data/dalvik-cache/arm/data@app@com.example.androidexplorer-1@base.apk@classes.dex.art
  &gt; 04-23 15:35:34.194: V/BitmapFactory(20672):
  &gt; DecodeImagePath(decodeResourceStream3) :
  &gt; res/drawable-xxhdpi-v4/ic_ab_back_holo_dark_am.png 04-23 15:35:34.204:
  &gt; V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) :
  &gt; res/drawable-xxhdpi-v4/sym_def_app_icon.png 04-23 15:35:34.234:
  &gt; D/AbsListView(20672): Get MotionRecognitionManager 04-23 15:35:34.244:
  &gt; V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) :
  &gt; res/drawable-xhdpi-v4/ic_launcher.png 04-23 15:35:34.254:
  &gt; V/Path:(20672): /storage/emulated/0 04-23 15:35:34.254:
  &gt; V/BEFORE(20672): Before Reading Fail... 04-23 15:35:34.254:
  &gt; E/File(20672): fail readDirectory() errno=13 04-23 15:35:34.254:
  &gt; V/AFTER(20672): After Reading Fail... 04-23 15:35:34.254:
  &gt; V/CRASH(20672): 1 Line before crash 04-23 15:35:34.254:
  &gt; D/AndroidRuntime(20672): Shutting down VM 04-23 15:35:34.254:
  &gt; E/AndroidRuntime(20672): FATAL EXCEPTION: main 04-23 15:35:34.254:
  &gt; E/AndroidRuntime(20672): Process: com.example.androidexplorer, PID:
  &gt; 20672 04-23 15:35:34.254: E/AndroidRuntime(20672):
  &gt; java.lang.RuntimeException: Unable to start activity
  &gt; ComponentInfo{com.example.androidexplorer/com.example.androidexplorer.MainActivity}:
  &gt; java.lang.NullPointerException: Attempt to get length of null array
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.app.ActivityThread.access$900(ActivityThread.java:172) 04-23
  &gt; 15:35:34.254: E/AndroidRuntime(20672):  at
  &gt; android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.os.Handler.dispatchMessage(Handler.java:102) 04-23
  &gt; 15:35:34.254: E/AndroidRuntime(20672):  at
  &gt; android.os.Looper.loop(Looper.java:145) 04-23 15:35:34.254:
  &gt; E/AndroidRuntime(20672):    at
  &gt; android.app.ActivityThread.main(ActivityThread.java:5834) 04-23
  &gt; 15:35:34.254: E/AndroidRuntime(20672):  at
  &gt; java.lang.reflect.Method.invoke(Native Method) 04-23 15:35:34.254:
  &gt; E/AndroidRuntime(20672):    at
  &gt; java.lang.reflect.Method.invoke(Method.java:372) 04-23 15:35:34.254:
  &gt; E/AndroidRuntime(20672):    at
  &gt; com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 04-23
  &gt; 15:35:34.254: E/AndroidRuntime(20672): Caused by:
  &gt; java.lang.NullPointerException: Attempt to get length of null array
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; com.example.androidexplorer.MainActivity.getDir(MainActivity.java:57)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; com.example.androidexplorer.MainActivity.onCreate(MainActivity.java:31)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.app.Activity.performCreate(Activity.java:6221) 04-23
  &gt; 15:35:34.254: E/AndroidRuntime(20672):  at
  &gt; android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
  &gt; 04-23 15:35:34.254: E/AndroidRuntime(20672):    ... 10 more
  </code></pre>
  </blockquote>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Error was in my manifest with the permissions. 
  What I had at the beginning:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.spicysoftware.infoid"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="15"
          android:targetSdkVersion="15" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"
           android:name="android.permission.READ_EXTERNAL_STORAGE"&gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".sysinfo"&gt;&lt;/activity&gt;
          &lt;activity android:name=".daten"&gt;&lt;/activity&gt;
          &lt;activity android:name=".storage"&gt;&lt;/activity&gt;
          &lt;activity android:name="org.achartengine.GraphicalActivity"&gt; &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>And Updated:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.spicysoftware.infoid"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="15"
          android:targetSdkVersion="15" /&gt;
  
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme"&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".sysinfo"&gt;&lt;/activity&gt;
          &lt;activity android:name=".daten"&gt;&lt;/activity&gt;
          &lt;activity android:name=".storage"&gt;&lt;/activity&gt;
          &lt;activity android:name="org.achartengine.GraphicalActivity"&gt; &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>