title: Android java.lang.NullPointerException: Attempt to get length of null array
body: <p>Sorry for my formatting I am newbie to android as well as to stackoverflow
  cant submit all of my logcat due to some formatting error</p>
  
  <p>in short I am getting following error: </p>
  
  <pre><code>at com.youmasti.mp3test.MainActivity.findsongs(MainActivity.java:45)
  at com.youmasti.mp3test.MainActivity.onCreate(MainActivity.java:27)
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>Main Activity
  
  public class MainActivity extends AppCompatActivity {
  
  ListView lv;
  String items;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      lv= (ListView)findViewById(R.id.lvPlaylist);
      //File fTest = Environment.getExternalStorageDirectory();
      ArrayList&lt;File&gt; mySongs = findsongs(Environment.getExternalStorageDirectory());
  
      for (int i = 0; i &lt; mySongs.size(); i++) {
          toast(mySongs.get(i).getName().toString());
      }
  }
  
  public ArrayList&lt;File&gt; findsongs(File root) {
      ArrayList&lt;File&gt; al= new ArrayList&lt;File&gt;();
      File[] file= root.listFiles();
  
      for (File singlefile : file) {
          if (singlefile.isDirectory() &amp;&amp; !singlefile.isHidden()) {
              al.addAll(findsongs(singlefile));
          } else {
              if (singlefile.getName().endsWith(".mp3")) {
                  al.add(singlefile);
              }
          }
      }
      return al;
  }
  
  public void toast (String text) {
      Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
  }
  </code></pre>
  
  <p>List item:</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.youmasti.mp3test"&gt;    
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/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;/application&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>LogCat
  
     08-30 10:39:30.227    2441-2441/? I/art﹕ Not late-enabling -Xcheck:jni        (already on)
     08-30 10:39:30.227    2441-2441/? I/art﹕ Late-enabling JIT
     08-30 10:39:30.229    2441-2441/? I/art﹕ JIT created with                code_cache_capacity=2MB compile_threshold=1000
     08-30 10:39:30.346    2441-2441/? W/System﹕ ClassLoader referenced        unknown path: /data/app/com.youmasti.mp3test-1/lib/x86
     08-30 10:39:30.527    2441-2441/com.youmasti.mp3test D/AndroidRuntime﹕        Shutting down VM
         --------- beginning of crash
     08-30 10:39:30.528    2441-2441/com.youmasti.mp3test E/AndroidRuntime﹕        FATAL EXCEPTION: main
         Process: com.youmasti.mp3test, PID: 2441
         java.lang.RuntimeException: Unable to start activity ComponentInfo       {com.youmasti.mp3test/com.youmasti.mp3test.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.youmasti.mp3test.MainActivity.findsongs       (MainActivity.java:45)
                 at com.youmasti.mp3test.MainActivity.onCreate       (MainActivity.java:27)
                 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)
  </code></pre>