Hi
I have below code and in below line it shows Null Value
(var item in tagResponse.Data.items)
C#
protected void MyOwnVideos11()
{
StringBuilder htmlTable = new StringBuilder();
string nextPageToken = string.Empty;
Int32 Sr = 1;
var data0 = GetVideosList(nextPageToken);
var orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt);
htmlTable.Append("");
htmlTable.Append("Sr.Video IdVideo TitleDescriptionPublished");
htmlTable.Append("");
nextPageToken = data0.nextPageToken;
while (!string.IsNullOrEmpty(nextPageToken))
{
data0 = GetVideosList(nextPageToken);
orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt);
nextPageToken = data0.nextPageToken;
foreach (var data in orderByPublishAt)
{
var clientTag = new RestClient("googleapis.com/youtube/v3/");
var tagRequest = new RestRequest("videos", Method.GET);
tagRequest.AddParameter("key", "AFG8");
tagRequest.AddParameter("part", "snippet,statistics");
tagRequest.AddParameter("id", data.id.videoId);
var tagResponse = clientTag.Execute(tagRequest);
foreach (var item in tagResponse.Data.items)
{
htmlTable.Append("" + Sr + "");
htmlTable.Append("");
htmlTable.Append("" + item.snippet.title + "");
htmlTable.Append("" + item.snippet.description + "");
htmlTable.Append("" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + "");
htmlTable.Append("");
Sr = Sr + 1;
}
}
htmlTable.Append("");
htmlTable.Append("");
PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });
}
-------------------------------------------
private static YoutubeSearchListResponse GetVideosList(string nextPageToken)
{
var client = new RestClient("googleapis.com/youtube/v3");
var request = new RestRequest("search", Method.GET);
request.AddParameter("part", "snippet");
request.AddParameter("type", "video");
request.AddParameter("channelId", "UVw");
request.AddParameter("key", "AFG8");
if (!string.IsNullOrEmpty(nextPageToken))
{
request.AddParameter("pageToken", nextPageToken);
}
var response = client.Execute(request);
return response.Data;
}Thanks

Tuhin PaulRank #7 · 71.9K reputationPosted Mar 29, 2025, 12:02 PM EDT
The issue you're encountering is likely due to the
tagResponse.Data.itemsbeingnull. This can happen for several reasons, such as an invalid API response, missing or incorrect parameters in the request, or an issue with the deserialization of the API response into theVideoListResponseobject.1. Debugging the Issue
To identify why
tagResponse.Data.itemsisnull, follow these steps:Step 1: Check the API Response
var tagResponse = clientTag.Execute(tagRequest); Console.WriteLine(tagResponse.Content); // Logs the raw JSON response Console.WriteLine(tagResponse.ErrorMessage); // Logs any error messagetagResponse.Contentis empty or contains an error, it indicates a problem with the API request.idparameter (e.g., invalid video ID).AFG8).Step 2: Validate the Deserialization
VideoListResponseclass matches the structure of the JSON response returned by the YouTube API.itemsproperty should be properly defined in theVideoListResponseclass:public class VideoListResponse { public List items { get; set; } } public class VideoItem { public Snippet snippet { get; set; } } public class Snippet { public string title { get; set; } public string description { get; set; } public DateTime publishedAt { get; set; } }itemsproperty is not correctly mapped, it will result innull.Tuhin PaulRank #7 · 71.9K reputationPosted Mar 29, 2025, 12:03 PM EDT
Step 3: Handle Null Responses Gracefully
tagResponse.Data.items, check if it isnull:2. Fixing the Code
Here’s the updated code with proper error handling and debugging:
protected void MyOwnVideos11() { StringBuilder htmlTable = new StringBuilder(); string nextPageToken = string.Empty; Int32 Sr = 1; var data0 = GetVideosList(nextPageToken); var orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt); htmlTable.Append(""); htmlTable.Append("Sr.Video IdVideo TitleDescriptionPublished"); htmlTable.Append(""); nextPageToken = data0.nextPageToken; while (!string.IsNullOrEmpty(nextPageToken)) { data0 = GetVideosList(nextPageToken); orderByPublishAt = data0.items.OrderBy(x => x.snippet.publishedAt); nextPageToken = data0.nextPageToken; foreach (var data in orderByPublishAt) { var clientTag = new RestClient("https://googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.GET); tagRequest.AddParameter("key", "AFG8"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("id", data.id.videoId); var tagResponse = clientTag.Execute(tagRequest); // Log the raw response for debugging Console.WriteLine(tagResponse.Content); Console.WriteLine(tagResponse.ErrorMessage); // Check if the response contains valid data if (tagResponse.Data?.items == null || tagResponse.Data.items.Count == 0) { Console.WriteLine($"No items found for video ID: {data.id.videoId}"); continue; // Skip to the next iteration } foreach (var item in tagResponse.Data.items) { htmlTable.Append(""); htmlTable.Append("" + Sr + ""); htmlTable.Append("" + data.id.videoId + ""); htmlTable.Append("" + item.snippet.title + ""); htmlTable.Append("" + item.snippet.description + ""); htmlTable.Append("" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + ""); htmlTable.Append(""); Sr = Sr + 1; } } } htmlTable.Append(""); htmlTable.Append(""); PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() }); }