2
Answers

Null Value

Ramco Ramco

Ramco Ramco

2w
126
1

Hi

  I have below code and in below line it shows Null Value

(var item in tagResponse.Data.items)

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("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
    htmlTable.Append("<thead><tr><th>Sr.</th><th>Video Id</th><th>Video Title</th><th>Description</th><th>Published</th></tr></thead>");
    htmlTable.Append("<tbody>");

    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<VideoListResponse>(tagRequest);

            foreach (var item in tagResponse.Data.items)
            {
                htmlTable.Append("<td >" + Sr + "</td>");
                htmlTable.Append("<td></td>");
                htmlTable.Append("<td >" + item.snippet.title + "</td>");
                htmlTable.Append("<td >" + item.snippet.description + "</td>");


                htmlTable.Append("<td >" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + "</td>");
                htmlTable.Append("</tr>");
                Sr = Sr + 1;

        }
    }
    htmlTable.Append("</tbody>");
    htmlTable.Append("</table>");
    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<YoutubeSearchListResponse>(request);

    return response.Data;
}
C#

Thanks

Answers (2)
1
Tuhin Paul

Tuhin Paul

42 33.2k 309.9k 2w

Step 3: Handle Null Responses Gracefully

  • Before iterating over tagResponse.Data.items, check if it is null:
if (tagResponse.Data?.items == null)
{
    Console.WriteLine("No items found in the response.");
    continue; // Skip to the next iteration
}

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("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
    htmlTable.Append("<thead><tr><th>Sr.</th><th>Video Id</th><th>Video Title</th><th>Description</th><th>Published</th></tr></thead>");
    htmlTable.Append("<tbody>");

    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<VideoListResponse>(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("<tr>");
                htmlTable.Append("<td>" + Sr + "</td>");
                htmlTable.Append("<td>" + data.id.videoId + "</td>");
                htmlTable.Append("<td>" + item.snippet.title + "</td>");
                htmlTable.Append("<td>" + item.snippet.description + "</td>");
                htmlTable.Append("<td>" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + "</td>");
                htmlTable.Append("</tr>");
                Sr = Sr + 1;
            }
        }
    }

    htmlTable.Append("</tbody>");
    htmlTable.Append("</table>");
    PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });
}
1
Tuhin Paul

Tuhin Paul

42 33.2k 309.9k 2w

The issue you're encountering is likely due to the tagResponse.Data.items being null. 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 the VideoListResponse object.

1. Debugging the Issue

To identify why tagResponse.Data.items is null, follow these steps:

Step 1: Check the API Response

  • Add logging or debugging statements to inspect the raw response from the YouTube API:
var tagResponse = clientTag.Execute<VideoListResponse>(tagRequest);
Console.WriteLine(tagResponse.Content); // Logs the raw JSON response
Console.WriteLine(tagResponse.ErrorMessage); // Logs any error message
    • If tagResponse.Content is empty or contains an error, it indicates a problem with the API request.
    • Common issues include:
      • Incorrect id parameter (e.g., invalid video ID).
      • Missing or incorrect API key (AFG8).
      • Rate limits exceeded.

Step 2: Validate the Deserialization

  • Ensure that the VideoListResponse class matches the structure of the JSON response returned by the YouTube API.
  • For example, the items property should be properly defined in the VideoListResponse class:
public class VideoListResponse
{
    public List<VideoItem> 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; }
}
  • If the items property is not correctly mapped, it will result in null.
Next Recommended Forum