Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:02:45+00:00 2026-06-06T20:02:45+00:00

I am making an android application that loads files from a directory on the

  • 0

I am making an android application that loads files from a directory on the sdcard into a list. Once an item is tapped, it resolves its path, and puts it into a string. I am trying to load the string into a mediaplayer with the .SetDataSource() method, but it force closes and causes an exception. How can i fix this? Please help me and thanks! Code is below:

package com.mysoftwaremobileapps.Cross;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MusicActivity extends ListActivity {
//Called when the activity is first created
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
  static final String[] COUNTRIES = new String[] {
      "Movies"
      };
  MediaPlayer mp1;
  String musicUri;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.musicscreen);

    //Setup ListView
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    File mFile = new File(Environment.getExternalStorageDirectory() + "/Music/");
    myList.addAll(Arrays.asList(mFile.list()));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));


    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        //When an item is clicked, play it on a MediaPlayer
          try {
              musicUri = Environment.getExternalStorageDirectory() + "/Music/" + myList.get(position);
            mp1.setDataSource(musicUri);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  }
    });
}
}

Logcat:

    07-03 21:25:42.031: D/ViewConsistency(5165): AbsListView android.widget.ListView@40588c48 enabled= true
07-03 21:25:42.051: D/ViewConsistency(5165): AbsListView android.widget.ListView@40588c48 enabled= true
07-03 21:25:43.691: D/AndroidRuntime(5165): Shutting down VM
07-03 21:25:43.691: W/dalvikvm(5165): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-03 21:25:43.721: E/AndroidRuntime(5165): FATAL EXCEPTION: main
07-03 21:25:43.721: E/AndroidRuntime(5165): java.lang.NullPointerException
07-03 21:25:43.721: E/AndroidRuntime(5165):     at com.mysoftwaremobileapps.Cross.MusicActivity$1.onItemClick(MusicActivity.java:52)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.widget.ListView.performItemClick(ListView.java:3513)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1877)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.os.Handler.handleCallback(Handler.java:587)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.os.Looper.loop(Looper.java:130)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at android.app.ActivityThread.main(ActivityThread.java:3835)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at java.lang.reflect.Method.invoke(Method.java:507)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
07-03 21:25:43.721: E/AndroidRuntime(5165):     at dalvik.system.NativeStart.main(Native Method)
07-03 21:25:45.171: I/Process(5165): Sending signal. PID: 5165 SIG: 9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:02:47+00:00Added an answer on June 6, 2026 at 8:02 pm

    You never instantiated the MediaPlayer object which is what is throwing the NPE you’re getting; you only left it as: MediaPlayer m1p

    Before setting the data source, you need to instantiate it with:

     m1p = new MediaPlayer();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an android application that will play an mp3 file once a
I'm making in house application that download data via xml files from external server.
I'm making an Android application that has to read some values from a JSON
I am making an android application that shows a list with users, I would
So I'm making this android application, that needs to read data from user-provided CSV
I am making an android application, which takes an image from camera and then
So, I'm making my first Android application, which basically contains ~250 .txt files in
I'm making an Android application that reads an XML Internet. This application uses SAX
I am making an android application that needs to use a ListView . I
I am making an android application that uses these views: (Root) - LinearLayout (Child

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.