title: App crashes on app exit if mediaplayer is not played
body: <p>My app crashes on app exit. Except if the song is playing and you exit, it will never crash. But if the song is NOT playing/played and i exit the app, it will exit and crash. I kill your task in the ondestroy function the activity.
  Please help me.</p>
  <p>This is the Tip1Activity.java</p>
  <pre><code>public class Tip1Activity extends AppCompatActivity {
      private ArrayList&lt;Music&gt; arrayList;
      private CustomMusicAdapter adapter;
      private RecyclerView recyclerView;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_tip1);
  
  
  
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  
          recyclerView =  findViewById(R.id.recyclerViewList);
  
          arrayList = new ArrayList&lt;&gt;();
          arrayList.add(new Music(&quot;001 Veni, veni, Emmanuel&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.veni_emmanuel));
          arrayList.add(new Music(&quot;002 O come. O come Emmanuel&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.o_come_emmanuel));
          arrayList.add(new Music(&quot;003 Savior of the nations, come&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.savior_of_nations));
          arrayList.add(new Music(&quot;004 On Jordan's bank the Baptist's cry&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.on_jordans_bank));
          arrayList.add(new Music(&quot;005 Lo, he comes&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.lo_he_comes));
          arrayList.add(new Music(&quot;006 O Word that goest forth on high&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.o_word_that_goes_forth));
          arrayList.add(new Music(&quot;007 Wake, awake&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.wake_awake));
          arrayList.add(new Music(&quot;008 Rorate caeli&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.rorate_caeli));
          arrayList.add(new Music(&quot;009 Conditor alme siderum&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.conditor_alme_siderum));
          arrayList.add(new Music(&quot;010 Creator of the stars of night&quot;, &quot;SONGS OF WORSHIP&quot;, R.raw.creator_of_the_stars));
  
  
  
          adapter = new CustomMusicAdapter(this, R.layout.custom_music_row, arrayList);
          recyclerView.setAdapter(adapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
          recyclerView.setHasFixedSize(false);
  
      }
  
  
  
      public void onDestroy() {
          super.onDestroy();
          if( adapter !=null ){
              adapter.release();
          }
      }
  
      @Override
      public void onBackPressed() {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setMessage(&quot;Do you want to close App?&quot;)
                  .setCancelable(false)
                  .setPositiveButton(&quot;Yes&quot;, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          Tip1Activity.this.finish();
                      }
                  })
                  .setNegativeButton(&quot;No&quot;, new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int id) {
                          dialog.cancel();
                      }
                  });
          AlertDialog alert = builder.create();
          alert.show();
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu, menu);
  
          MenuItem menuItem = menu.findItem(R.id.search_view);
  
          SearchView searchView = (SearchView) menuItem.getActionView();
  
          searchView.setMaxWidth(Integer.MAX_VALUE);
  
          searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String query) {
                  return false;
              }
  
              @Override
              public boolean onQueryTextChange(String newText) {
  
                  adapter.getFilter().filter(newText);
                  return true;
              }
          });
  
  
  
          return  true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()){
  
              case R.id.item1:
                  Toast.makeText(this, &quot;Settings Selected&quot;, Toast.LENGTH_SHORT).show();
                  startActivity(new Intent(this, Main2Activity.class));
                  return true;
  
              //DO YOUR FUNCTIONALITY HERE
  
  
  
  
              default:
                  return super.onOptionsItemSelected(item);
          }
  
      }
  }
  </code></pre>
  <p>The CustomMusicAdapter.java</p>
  <pre><code>public class CustomMusicAdapter extends RecyclerView.Adapter&lt;MyViewHolder&gt; implements Filterable {
  
      private Context context;
      private int layout;
      private ArrayList&lt;Music&gt; myArraylist;
      private ArrayList&lt;Music&gt; getUserModelListFiltered;
      private static MediaPlayer mediaPlayer;
      private boolean flag=true;
  
  
      public CustomMusicAdapter(Context context, int custom_music_row, ArrayList&lt;Music&gt; myArraylist) {
          this.context = context;
          this.layout = layout;
          this.getUserModelListFiltered = myArraylist;
          this.myArraylist = myArraylist;
  
      }
  
      public void release(){
          mediaPlayer.stop();
          mediaPlayer.release();
      }
  
  
      @NonNull
      @Override
      public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          final View itemView = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.custom_music_row, parent, false);
          return new MyViewHolder(itemView);
      }
  
  
  
      @SuppressLint(&quot;RecyclerView&quot;)
      @Override
      public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
          holder.setIsRecyclable(false);
  
          final Music music = myArraylist.get(position);
          holder.mTitle.setText(music.getName());
          holder.mDes.setText(music.getSinger());
  
          //Play Music Setup
          holder.mImageaplay.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  if (flag){
                      mediaPlayer=MediaPlayer.create(context,music.getSong());
                      flag=false;
                  }
                  if (mediaPlayer.isPlaying()){
                      mediaPlayer.pause();
                      mediaPlayer.start();
                      holder.mImageaplay.setImageResource(R.drawable.ic_play);
                  }else {
                      mediaPlayer.start();
                      holder. mImageaplay.setImageResource(R.drawable.ic_pause);
                  }
                  mediaPlayer.start();
  
              }
          });
  
          // stop player
          holder.mImageStop.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (!flag){
                      mediaPlayer.stop();
                      mediaPlayer.release();
                      flag=true;
                  }
                  holder.mImageaplay.setImageResource(R.drawable.ic_play);
              }
          });
  
      }
  
      @Override
      public int getItemCount() {
          return myArraylist.size();
      }
  
      @Override
      public Filter getFilter() {
  
          Filter filter = new Filter() {
              @Override
              protected FilterResults performFiltering(CharSequence charSequence) {
                  FilterResults filterResults = new FilterResults();
                  if (charSequence == null | charSequence.length() == 0){
                      filterResults.count = getUserModelListFiltered.size();
                      filterResults.values = getUserModelListFiltered;
                  }else {
                      String searchChr = charSequence.toString().toLowerCase();
                      List&lt;Music&gt; resultData = new ArrayList&lt;&gt;();
                      for (Music music: getUserModelListFiltered){
                          if (music.getName().toLowerCase().contains(searchChr)){
                              resultData.add(music);
                          }
                      }
                      filterResults.count = resultData.size();
                      filterResults.values = resultData;
                  }
                  return filterResults;
              }
  
              @Override
              protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
  
                  myArraylist = (ArrayList&lt;Music&gt;) filterResults.values;
                  notifyDataSetChanged();
  
              }
          };
          return filter;
      }
  
      public class ViewHolder {
          TextView textView_SongName, textView_artist;
          ImageView imageView_play, imageView_stop;
          private View convertview;
          LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  
          public ViewHolder() {
              convertview = layoutInflater.inflate(layout, null);
              textView_SongName = convertview.findViewById(R.id.textName);
              textView_artist = convertview.findViewById(R.id.txtSinger);
              imageView_play = convertview.findViewById(R.id.ivPlay);
              imageView_stop = convertview.findViewById(R.id.ivStop);
  
          }
      }
  }
  </code></pre>
  <p>This is the <strong>LogaCat</strong></p>
  <pre><code>2020-06-24 12:44:35.598 27137-27137/com.josephgritchen.songsofworship E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.josephgritchen.songsofworship, PID: 27137
      java.lang.RuntimeException: Unable to destroy activity {com.josephgritchen.songsofworship/com.josephgritchen.songsofworship.Tip1Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
          at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4425)
          at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4443)
          at android.app.ActivityThread.-wrap5(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1684)
          at android.os.Handler.dispatchMessage(Handler.java:106)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6549)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
          at com.josephgritchen.songsofworship.CustomMusicAdapter.release(CustomMusicAdapter.java:38)
          at com.josephgritchen.songsofworship.Tip1Activity.onDestroy(Tip1Activity.java:67)
          at android.app.Activity.performDestroy(Activity.java:7239)
          at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1249)
          at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4412)
          at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4443) 
          at android.app.ActivityThread.-wrap5(Unknown Source:0) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1684) 
          at android.os.Handler.dispatchMessage(Handler.java:106) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6549) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888) 
  </code></pre>
  <p><strong>This is what i did that still gave me error</strong></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// stop player
          holder.mImageStop.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(mediaPlayer!=null)
                  {
                      if(mediaPlayer.isPlaying())
                      {
                          mediaPlayer.stop();
                          mediaPlayer.release();
                      }
                  holder.mImageaplay.setImageResource(R.drawable.ic_play);
              }}
  
          });
  
      }</code></pre>
  </div>
  </div>
  </p>