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
Jignesh KumarPosted Dec 12, 2022, 3:12 AM
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,
Ajay SinghPosted Dec 13, 2022, 6:14 AM
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
}
Jignesh KumarPosted Dec 12, 2022, 4:13 AM
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
Jignesh KumarPosted Dec 12, 2022, 3:45 AM
Hello Ramco,
I have done change in below snippet,
Ramco RamcoPosted Dec 12, 2022, 3:37 AM
Hi Jignesh
What changes i have to do
Thanks
Ramco RamcoPosted Dec 11, 2022, 10:57 AM
Hi
On channelsListResponse.Items it is giving error.
In forusername i am giving my UserID . Is it o.k
Thanks
Aravind GovindarajPosted Dec 11, 2022, 10:03 AM
Can u pls check r u getting value in response. Before for loop pls do null check to avoid object reference error