NullPointerException when using MediaMetadataRetriever from loop with ArrayList
<p>I found this example: <a href="http://xjaphx.wordpress.com/2011/06/28/query-mp3-information-using-android-supported-function/" rel="nofollow">http://xjaphx.wordpress.com/2011/06/28/query-mp3-information-using-android-supported-function/</a> for MediaMetadataRetriever, which works fine. But when I try to use it form a loop and add the data to an ArrayList I get this error:</p>

<pre><code>04-25 20:20:27.121: E/AndroidRuntime(20692): FATAL EXCEPTION: main
04-25 20:20:27.121: E/AndroidRuntime(20692): java.lang.NullPointerException
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ListView.fillDown(ListView.java:674)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ListView.fillFromTop(ListView.java:735)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.ListView.layoutChildren(ListView.java:1652)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.Choreographer.doFrame(Choreographer.java:532)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.os.Handler.handleCallback(Handler.java:725)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.os.Looper.loop(Looper.java:137)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at android.app.ActivityThread.main(ActivityThread.java:5039)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at java.lang.reflect.Method.invokeNative(Native Method)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at java.lang.reflect.Method.invoke(Method.java:511)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 20:20:27.121: E/AndroidRuntime(20692):    at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>This is the Class with the MediaMetadataRetriever:</p>

<pre><code>    public class GenreListing extends Activity {

           ListView listView;
            ArrayAdapter&lt;String&gt; adapter;

            private SongsManager songManager;
            private Utilities utils;            
            private List&lt;String&gt; songs = new ArrayList&lt;String&gt;();       
            private static final String SD_PATH = new String ("/sdcard/");

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.genre_list);

            ListView lv = (ListView) findViewById(R.id.genreOutput);

            ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
                android.R.layout.simple_list_item_1, songs);        

            lv.setAdapter(adapter);

            songManager = new SongsManager();
            utils = new Utilities();


                updatePlaylist();

    }   

         private void updatePlaylist() {
                File home = new File(SD_PATH);
                String out = "";
                if (home.listFiles(new Mp3Filter()).length &gt; 0) {
                    for (File file : home.listFiles(new Mp3Filter())) {
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(file.getPath());

                        String genre = "";
                        //String genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
                        genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);   


                    songs.add(genre);
                    mmr.release();

                    }               

            }
    }
</code></pre>

<p>}</p>
