Nullpointer exception at ArrayAdapter
<p>I do have an app which loads sounds from sdCard and shows it in AlertDialog as a <code>list</code>. It loads and plays sounds perfect. Now, I want to limit the amount of sounds to be loaded in the <code>list</code>. So, here is a task:</p>

<blockquote>
  <p>I do have N sounds in folder at sdCard. I do have a list, where based
  on condition I want to show A amount of sounds. The condition is: if
  condition1>6 and condition2&lt;40 then it's false and show 6 of sounds in
  a list, otherwise if condition is true, show all sounds in a list.</p>
</blockquote>

<p>Here is how I implemented it in a code, but it gives to me <code>NullPointerException</code>.</p>

<pre><code>ins_mp3List = ins_Dir.listFiles(new Mp3Filter()); //list of all sounds in directory(N)

 user_instruments = new String[ins_mp3List.length]; //will be used in a system internally
 user_instruments_toShow =new String[ins_mp3List.length]; //will be shown in the list
 soundsQ_check=Integer.valueOf(custom_sounds.getString("RAW_1_1", ""));// the value is 0, it is checked
//Condition
 if (ins_mp3List.length&gt;6 &amp;&amp; soundsQ_check&lt;40){ 
             user_instruments_block=true;
             for (int i=0; i&lt;= 5; i++) {
              String [] seperated = ins_mp3List[i].toString().split(directory);
              user_instruments[i]=ins_mp3List[i].toString();
              user_instruments_toShow[i] = seperated[1];    
              Log.v("Instruments","true");
             }

         }else{
             for (int i=0; i&lt; ins_mp3List.length; i++) {

              user_instruments_block=false;
              String [] seperated = ins_mp3List[i].toString().split(directory);
              user_instruments[i]=ins_mp3List[i].toString();
              user_instruments_toShow[i] = seperated[1];            
            Log.v("Instruments", user_instruments[i]);
               }
</code></pre>

<p>In the sdCard folder there are 8 files. <code>soundQ_check=0</code>.</p>

<p>LogCat gives error:</p>

<pre><code>01-27 19:52:11.652: E/AndroidRuntime(2667): FATAL EXCEPTION: main
01-27 19:52:11.652: E/AndroidRuntime(2667): java.lang.NullPointerException
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.AbsListView.obtainView(AbsListView.java:2019)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ListView.onMeasure(ListView.java:1155)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.os.Looper.loop(Looper.java:137)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>I think that somewhere condition is wrong... Any help would be much appreciated. If, the amount of sounds less then 6 it works fine, but if it is more then 6 it gives nullPointerException...</p>

<p>UPD LAST: I've found a solution. I was not supposed to put </p>

<pre><code> user_instruments = new String[ins_mp3List.length]; //will be used in a system internally
 user_instruments_toShow =new String[ins_mp3List.length]; //will 
</code></pre>

<p>outside the condition. These lines supposed to go under condition. Because of length varies it was giving to me error. Actual length was 8, and I was initializing only 6 of them. If to put that both strings under conditions it will work. Thanks for comments!</p>
