Ramco Ramco

Ramco Ramco

  • 443
  • 3.4k
  • 533.7k

Object reference not set to an instance of the object

Dec 11 2022 7:00 AM

Hi

  I have below code but it is giving error - Object reference not set to an instance of the object on this line

foreach (var channel in channelsListResponse.Items)

In forusername i am giving my UserID

var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyAu9f5blF7idzt6cG2l29vQ9g4HBdYy36H" });
                var channelsListRequest = yt.Channels.List("contentDetails");
                channelsListRequest.ForUsername = "g3ZrkCBQ3GngW-m_nKoaSw";
                var channelsListResponse = channelsListRequest.Execute();
                int VideoCount = 1;
                foreach (var channel in channelsListResponse.Items)
                {
                    var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
                    var nextPageToken = "";
                    while (nextPageToken != null)
                    {
                        var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
                        playlistItemsListRequest.PlaylistId = uploadsListId;
                        playlistItemsListRequest.MaxResults = 50;
                        playlistItemsListRequest.PageToken = nextPageToken;
 
                        var playlistItemsListResponse = playlistItemsListRequest.Execute();
                        foreach (var playlistItem in playlistItemsListResponse.Items)
                        {
                            Console.WriteLine("Sl No={0}", VideoCount);
                            Console.Write("Video ID ={0} ", "youtube.com/embed/" + playlistItem.Snippet.ResourceId.VideoId);

                            VideoCount++;
                        }
                        nextPageToken = playlistItemsListResponse.NextPageToken;
                    }
                    Console.ReadLine();
                }

Thanks


Answers (7)