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
C#
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

Jignesh KumarRank #91 · 22.9K reputationPosted Dec 11, 2022, 10:12 PM EST
Hello Ramco,
You need to check null refernce for channelsListResponse before accessing items property, you are trying to access from of null response that the reason you are getting this error,
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; if(channelsListResponse != null) { 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(); } }Jignesh KumarRank #91 · 22.9K reputationPosted Dec 11, 2022, 11:13 PM EST
Hello,
ForUserName parameter specifies a YouTube username, thereby requesting the channel associated with that username.
Please refer this one,
https://developers.google.com/youtube/v3/docs/channels/list
Aravind GovindarajRank #430 · 5.8K reputationPosted Dec 11, 2022, 5:03 AM EST
Can u pls check r u getting value in response. Before for loop pls do null check to avoid object reference error
Ajay SinghRank #1,163 · 1.6K reputationPosted Dec 13, 2022, 1:14 AM EST
Hi Ramco,
There might a chance that "channelsListResponse" object can give this exception. You can handle it by checking with If condition.
If(channelsListResponse)
{
// Do you logic here
}
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 11, 2022, 5:57 AM EST
Hi
On channelsListResponse.Items it is giving error.
In forusername i am giving my UserID . Is it o.k
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Dec 11, 2022, 10:37 PM EST
Hi Jignesh
What changes i have to do
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 11, 2022, 10:45 PM EST
Hello Ramco,
I have done change in below snippet,