MediaPlayer isn't equal to null, but I still get null pointer exception when I try to use it

41 Views Asked by At

I am making a music playing app in Android Java. I have a MediaPlayer object that is supposed to play the audio file selected in the default Android file chooser. This is how I am calling the file chooser:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Files"), Constants.FILE_PICKER_CODE);

And this is how I'm handling the callback:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constants.FILE_PICKER_CODE && resultCode == RESULT_OK) {
            // Get the data from the file picker.
            ClipData clipData = data.getClipData();
            if (clipData != null) {
                // Init queue var if necessary.
                if (queueTracks == null) { queueTracks = new ArrayList<>(); }

                // Clear current queue. TODO: Will change this when implementing multiple queues.
                queueTracks.clear();

                // Add the tracks to the queue list.
                for (int i = 0; i < clipData.getItemCount(); i++) {
                    Uri uri = clipData.getItemAt(i).getUri();
                    // Add the URI's path to queue list.
                    queueTracks.add(new File(uri.getPath()).getPath());
                    Snackbar.make(parentLayout, new File(uri.getPath()).getPath(), Snackbar.LENGTH_SHORT).show();
                }

                // Now try to play the first track.
                try {
                    // TESTING... will try to play one track from the device's storage. THIS IS WHERE THINGS ARE GOING WRONG.
                    Uri audioUri = Uri.fromFile(new File(queueTracks.get(0)));
                    mainMediaPlayer = MediaPlayer.create(this, audioUri);

                    if (mainMediaPlayer == null) {
                        Log.d("Media player", "null!!!");
                    }
                    mainMediaPlayer.start();

                    queueMode = true;
                    placeInQueue = 0;
                }
                catch (Exception e) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error");
                    builder.setMessage(Arrays.toString(e.getStackTrace()));
                    builder.show();

                    e.printStackTrace();
                    return;
                }
            }

            else {
                // Single track mode. THIS WORKS FINE!
                Uri audioUri = data.getData();
                mainMediaPlayer = MediaPlayer.create(this, audioUri);
                mainMediaPlayer.start();

                queueMode = false;
                placeInQueue = 0;
            }
            mainTrackSeekbar.setMax(mainMediaPlayer.getDuration());
            mainTrackSeekbar.setEnabled(true);
        }

I am receiving this unusual error, however:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

The error is being thrown from the line that is mainMediaPlayer.start(); on the if branch that is run when the user picks multiple files.

I logged what my mainMediaPlayer variables was, here are the result:

mainMediaPlayer = null--checked on the if branch that is run when the user picks multiple files. EVEN AFTER initializing it and running MediaPlayer.create() it is still null for some reason.

For some reason, when I select one file it works fine, but when I select multiple it crashes with the null pointer exception above. How do I fix this and make the MediaPlayer play the first item the user selected?

0

There are 0 best solutions below