@@ -514,7 +514,7 @@ public async Task<Mediafile> GetUpcomingSong(bool isNext = false)
514514 }
515515 else if ( IsSourceGrouped )
516516 {
517- toPlayFile = GetNextOrPrevSongInGroup ( false ) ;
517+ toPlayFile = GetNextOrPrevSongInGroup ( false ) ?? throw new NullReferenceException ( "Cannot get next song in group." ) ;
518518 }
519519 else
520520 {
@@ -569,27 +569,34 @@ private void UpdateCurrentlyPlayingSongIndex()
569569 }
570570 private Mediafile GetNextOrPrevSongInGroup ( bool prev = false )
571571 {
572- //get current group (the group in which current song is playing).
573- Grouping < IGroupKey , Mediafile > currentGroup = TracksCollection . GetCurrentlyPlayingGroup ( ) ;
574-
575- //get the index of the song playing in the currentGroup (with reference to the currentGroup)
576- int currentSongIndex = currentGroup . GetPlayingSongIndexInGroup ( ) ;
577-
578- //get next song index (depending on the parameters).
579- int nextSongIndex = prev ? currentSongIndex - 1 : currentSongIndex + 1 ;
580-
581- //get condition for next/prev group.
582- bool nextGroupCondition = nextSongIndex . Equals ( prev ? - 1 : currentGroup . Count ) ;
583-
584- //get next/prev group index
585- int nextGroupIndex = prev ? TracksCollection . IndexOf ( currentGroup ) - 1 : TracksCollection . IndexOf ( currentGroup ) + 1 ;
586-
587- //get next/prev group.
588- Grouping < IGroupKey , Mediafile > nextGroup = nextGroupCondition ? TracksCollection . ElementAt ( nextGroupIndex ) : currentGroup ;
589-
590- //get nextSong index depending on if the group is new or old.
591- int toPlaySongIndex = nextGroup . Equals ( currentGroup ) ? nextSongIndex : 0 ;
592- return nextGroup . ElementAt ( toPlaySongIndex ) ;
572+ try
573+ {
574+ //get current group (the group in which current song is playing).
575+ Grouping < IGroupKey , Mediafile > currentGroup = TracksCollection . GetCurrentlyPlayingGroup ( ) ;
576+
577+ //get the index of the song playing in the currentGroup (with reference to the currentGroup)
578+ int currentSongIndex = currentGroup . GetPlayingSongIndexInGroup ( ) ;
579+
580+ //get next song index (depending on the parameters).
581+ int nextSongIndex = prev ? currentSongIndex - 1 : currentSongIndex + 1 ;
582+
583+ //get condition for next/prev group.
584+ bool nextGroupCondition = nextSongIndex . Equals ( prev ? - 1 : currentGroup . Count ) ;
585+
586+ //get next/prev group index
587+ int nextGroupIndex = prev ? TracksCollection . IndexOf ( currentGroup ) - 1 : TracksCollection . IndexOf ( currentGroup ) + 1 ;
588+
589+ //get next/prev group.
590+ Grouping < IGroupKey , Mediafile > nextGroup = nextGroupCondition ? TracksCollection . ElementAt ( nextGroupIndex ) : currentGroup ;
591+
592+ //get nextSong index depending on if the group is new or old.
593+ int toPlaySongIndex = nextGroup . Equals ( currentGroup ) ? nextSongIndex : 0 ;
594+ return nextGroup . ElementAt ( toPlaySongIndex ) ;
595+ }
596+ catch ( NullReferenceException )
597+ {
598+ return null ;
599+ }
593600 }
594601
595602 private async void PlayNext ( )
0 commit comments