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

0

I'm learning Java and I know basically what NullPointerException means. But I would like to understand why this occurs.

This is the Activity code:

package com.example.android.miwok;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class FamilyActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;

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

        //Instantiate an ArrayList
        final ArrayList<Words> famillyArray = new ArrayList<Words>( );

        //Supply Array
        famillyArray.add(new Words("father", "әpә", R.drawable.family_father, R.raw.family_father));
        famillyArray.add(new Words("mother", "әṭa", R.drawable.family_mother, R.raw.family_mother));
        famillyArray.add(new Words("son", "angsi", R.drawable.family_son, R.raw.family_son));
        famillyArray.add(new Words("daughter", "tune", R.drawable.family_daughter, R.raw.family_daughter));
        famillyArray.add(new Words("older brother", "taachi", R.drawable.family_older_brother, R.raw.family_older_brother));
        famillyArray.add(new Words("younger brother", "chalitti", R.drawable.family_younger_brother, R.raw.family_younger_brother));
        famillyArray.add(new Words("older sister", "teṭe", R.drawable.family_older_sister, R.raw.family_older_sister));
        famillyArray.add(new Words("younger sister", "kolliti", R.drawable.family_younger_sister, R.raw.family_younger_sister));
        famillyArray.add(new Words("grandmother ", "ama", R.drawable.family_grandmother, R.raw.family_grandmother));
        famillyArray.add(new Words("grandfather", "paapa", R.drawable.family_grandfather, R.raw.family_grandfather));

        final WordsAdapter famillyAdapter = new WordsAdapter(this, famillyArray, R.color.category_family);
        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(famillyAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener( ) {
            @Override
            public void onItemClick( AdapterView<?> adapterView, View view, int position, long l ) {
                Words getSong = famillyArray.get(position);
                mediaPlayer = MediaPlayer.create(FamilyActivity.this, getSong.getSongResource());
                releaseMediaPlayer();
                mediaPlayer.start();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener( ) {
                    @Override
                    public void onCompletion( MediaPlayer mediaPlayer ) {
                        releaseMediaPlayer();
                    }
                });
            }
        });
    }
    private void releaseMediaPlayer(){
        if (mediaPlayer != null){
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

Notice this line of code above, is in the second @Override:

mediaPlayer = MediaPlayer.create(FamilyActivity.this, getSong.getSongResource());

The error is solved by simply putting MediaPlayer of variable mediaPlayer thus getting MediaPlayer mediaPlayer = Media...

But what I do not understand is: If I've instantiated the variable as an object of the MediaPlayer class at the beginning of the Activity, why do I have to declare again? If you look closely, the first line after public class FamillyActivy extends AppCompatActivity { is already declared.

Thanks for any help that makes me understand the logic of this error.

    
asked by anonymous 22.01.2018 / 07:35

1 answer

5

The error is not in declaring the variable mediaPlayer inside or outside the onItemClick method.

The error is in method releaseMediaPlayer . Note that this method is called after you instantiate the MediaPlayer object in the global variable of the same name.

mediaPlayer = MediaPlayer.create(FamilyActivity.this, getSong.getSongResource());
releaseMediaPlayer();

Now notice that the releaseMediaPlayer method is simply assigning to the global variable mediaPlayer , the value of null .

This method is removing the instance that you "just did" before you accessed it.

private void releaseMediaPlayer(){
    if (mediaPlayer != null){
        mediaPlayer.release();
        mediaPlayer = null; /* O erro é devido essa linha tornar a variável 'mediaPlayer' nula depois de ter sido instanciada */
    }
}

When you set the mediaPlayer variable within the onItemClick method, you stop using the global variable and start using a local variable.

And because a local variable can not be changed or accessed outside of the method where it was defined, the releaseMediaPlayer method can not make it null (sending error NullPointerException ) .

Although the error is in mediaPlayer.start(); , it is only caused because the variable is being set to null in the method already quoted

    
22.01.2018 / 11:15