I am getting the nullPointerException using this code in my player Activity
when i click next button everything works good until 8 or 9 items
from start of recycler view but after that this error comes .
I am creating a music player app everything working was great until i added the GradientDrawable
to fetch the color from image using palette and applied it to the baground drawable which is having a shape of gradient
color
//nextbuttonclicke method code snippet
uri1 = Uri.parse(listsong.get(position).getPath());
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri1);
metaDataMethod(uri1);
sName = listsong.get(position).getTitle();
song_name.setText(sName);
if (mediaPlayer == null)
mediaPlayer = MediaPlayer.create(PlayerActivity.this, uri1);
seekBar.setMax(mediaPlayer.getDuration() / 1000);
PlayerActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if(mediaPlayer != null){
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 200);
}
});
//MediaMetaData to set album art
mediaPlayer.setOnCompletionListener(this);
//this is the end of album art
pause_play.setBackgroundResource(R.drawable.ic_pause);
mediaPlayer.start();
metadataMethod is below
public void metaDataMethod(Uri uri) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uri.toString());
String totalout = "";
String totaloutNew = "";
// get mp3 info
// convert duration to minute:seconds
String duration =
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long dur = Long.parseLong(duration);
String seconds = String.valueOf((dur % 60000) / 1000);
String minutes = String.valueOf(dur / 60000);
totalout = minutes + ":" + seconds;
totaloutNew = minutes + ":" + "0" + seconds;
if (seconds.length() == 1) {
duration_total.setText(totaloutNew);
}else {
duration_total.setText(totalout);
}
byte[] art = retriever.getEmbeddedPicture();
final Bitmap image;
if( art != null ){
Glide.with(PlayerActivity.this).asBitmap()
.load(art).into(cover_art);
image = BitmapFactory.decodeByteArray(art, 0, art.length);
Palette.from(image).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(@Nullable Palette palette) {
Palette.Swatch vibranSwatch = null;
Palette.Swatch vibranLigt = null;
if (palette != null) {
vibranSwatch = palette.getVibrantSwatch();
}
if (palette != null)
{
vibranLigt = palette.getLightVibrantSwatch();
}
//you can generate as many as you want..
if (vibranSwatch != null)
{
ImageView imgIcon = findViewById(R.id.imageView);
RelativeLayout mContainer = findViewById(R.id.mContainer);
mContainer.setBackgroundResource(R.drawable.main_bg);
imgIcon.setBackgroundResource(R.drawable.gredient_bg);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{vibranSwatch.getRgb(),
0x00000000});
imgIcon.setBackground(gd);
GradientDrawable gdContainer = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{vibranSwatch.getRgb(), vibranSwatch.getRgb()});
mContainer.setBackground(gdContainer);
}
else if (vibranLigt != null)
{
ImageView imgIcon = findViewById(R.id.imageView);
RelativeLayout mContainer = findViewById(R.id.mContainer);
mContainer.setBackgroundResource(R.drawable.main_bg);
imgIcon.setBackgroundResource(R.drawable.gredient_bg);
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{vibranLigt.getRgb(),
0x00000000});
imgIcon.setBackground(gd);
GradientDrawable gdContainer = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP,
new int[]{vibranLigt.getRgb(), vibranLigt.getRgb()});
mContainer.setBackground(gdContainer);
}
else
{
}
}
});
}
else{
Glide.with(PlayerActivity.this)
.asBitmap()
.load(R.drawable.singer)
.into(cover_art);
}
// close object
retriever.release();
}
This is the logcat that i have seen in logcat:
04-29 13:43:45.330 7030-7030/com.aman.playmusix E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aman.playmusix, PID: 7030
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getDuration()' on a null object reference
at com.aman.playmusix.PlayerActivity.nextBtnClicked(PlayerActivity.java:264)
at com.aman.playmusix.PlayerActivity.access$300(PlayerActivity.java:38)
at com.aman.playmusix.PlayerActivity$9$1.onClick(PlayerActivity.java:237)
at android.view.View.performClick(View.java:4799)
at android.view.View$PerformClick.run(View.java:20042)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Any Help will be appreciated thank you.