NullPointerException when calling getPlaylist() method from another activity in Android. The method is supposed to get the audio info then return it using Cursor cursor = getContentResolver().query(MediaStore...)
Here are the details.
I'm working on a custom audio player using two activities (AudioPlayerActivity, FileManager). AudioPlayerActivity gets a list of audio files using FileManager**, then play them.
Here are snippets of them. The following is AudioPlayerActivity.java
ArrayList<HashMap<String, String>> audioList = new ArrayList<HashMap<String, String>>();
mp = new MediaPlayer();
fileManager = new FileManager();
audioList = fileManager.getPlaylist(); // NPE when calling this method!
playAudio(0);
and the next is FileManager.java, which reads all audio files and store the details(like MP3 ID tags) in ArrayList using MediaStore API.
public class FileManager extends Activity {
@Override
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(savedInstanceState);
// no view. just getting audio files
}
public ArrayList<HashMap<String, String> getPlaylist() {
String[] proj = { MediaStore.Audio.Media._ID };
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null}
Log.d("debug", "is cursor?"); // NPE here!
return list;
}
LogCat
09-30 11:55:35.265: D/AndroidRuntime(13516): Shutting down VM
09-30 11:55:35.265: W/dalvikvm(13516): threadid=1: thread exiting with uncaught exception (group=0x41d37c08)
09-30 11:55:35.270: E/AndroidRuntime(13516): FATAL EXCEPTION: main
09-30 11:55:35.270: E/AndroidRuntime(13516): Process: project.sample.audioplayer, PID: 13516
09-30 11:55:35.270: E/AndroidRuntime(13516): java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sample.audioplayer/project.sample.audioplayer.AudioPlayerActivity}: java.lang.NullPointerException
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread.access$900(ActivityThread.java:161)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.os.Handler.dispatchMessage(Handler.java:102)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.os.Looper.loop(Looper.java:157)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread.main(ActivityThread.java:5356)
09-30 11:55:35.270: E/AndroidRuntime(13516): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 11:55:35.270: E/AndroidRuntime(13516): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 11:55:35.270: E/AndroidRuntime(13516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-30 11:55:35.270: E/AndroidRuntime(13516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-30 11:55:35.270: E/AndroidRuntime(13516): at dalvik.system.NativeStart.main(Native Method)
09-30 11:55:35.270: E/AndroidRuntime(13516): Caused by: java.lang.NullPointerException
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:104)
09-30 11:55:35.270: E/AndroidRuntime(13516): at project.sample.audioplayer.FileManager.getPlaylist(FileManager.java:35)
09-30 11:55:35.270: E/AndroidRuntime(13516): at project.sample.audioplayer.AudioPlayerActivity.onCreate(AudioPlayerActivity.java:70)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.Activity.performCreate(Activity.java:5426)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-30 11:55:35.270: E/AndroidRuntime(13516): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
09-30 11:55:35.270: E/AndroidRuntime(13516): ... 11 more
It gives NullPointerException at line that having getContentResolver().query().
What am I missing? or is it not possible to use Cursor cursor = getContentResolver().query(MediaStore...) in a method instead of in onCreate() method?
I'm pretty sure that AudioPlayerAcitivity.java is not the problem as it works when using File class instead of MediaStore in FileManger.java.
getContentResolver()
needs to be called with proper context.
Instead of extending FileManager from Activity, just pass in the context from the calling activity (AudioPlayerActivity.java), then you can use the context in Cursor cursor = context.getContentResolver()...