0

I am using C# and using the YouTube V3 API. I am trying to insert a comment into a Video however whenever I do I receive a exception of "{"Object reference not set to an instance of an object."}" This happens whenever I run anything similar to the above code:

public void AddComment()
{
    CommentThread commentToAdd = new CommentThread();
    commentToAdd.Snippet.IsPublic = true;
    commentToAdd.Snippet.TopLevelComment.Snippet.TextOriginal = "Test";
    commentToAdd.Snippet.VideoId = "kc-LBxBcyG8";
    commentToAdd.Snippet.TopLevelComment.Snippet.VideoId = "kc-LBxBcyG8";
    CommentThreadsResource.InsertRequest ins = JKYouTube.NewYouTubeService().CommentThreads.Insert(commentToAdd, "snippet");
    var insertedComment = ins.Execute();
}

I am comparing this to the google explorer and using the same properties and the explorer actually adds the comments where as my program just fails. https://developers.google.com/youtube/v3/docs/commentThreads/insert

As soon as it reaches the second line of code of commentToAdd.Snippet.IsPublic = true;

It will just error and continue for every line above.

Any help would be greatly appreciated.

wkl
  • 77,184
  • 16
  • 165
  • 176

2 Answers2

2

Your problem lies in the fact Snippet is null.

Taken from the API link that you have given, you need to first create a CommentSnippet.

In the example that Google has provided:

// Insert channel comment by omitting videoId.
// Create a comment snippet with text.
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.setTextOriginal(text);

First, a CommentSnippet is created with some text and then we create a top level comment:

// Create a top-level comment with snippet.
Comment topLevelComment = new Comment();
topLevelComment.setSnippet(commentSnippet);

And then, you add your topLevelComment to the CommentThreadSnippet:

// Create a comment thread snippet with channelId and top-level
// comment.
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.setChannelId(channelId);
commentThreadSnippet.setTopLevelComment(topLevelComment);

When finally you have your CommentThreadSnippet, you can add it to a CommentThread:

// Create a comment thread with snippet.
CommentThread commentThread = new CommentThread();
commentThread.setSnippet(commentThreadSnippet);

Following those steps should not give you a NRE

Nikola
  • 2,093
  • 3
  • 22
  • 43
0

Many thanks for your help. Managed to complete it.

async Task AddVideoCommentAsync(string commentToAdd, string videoID)
    {
        CommentSnippet commentSnippet = new CommentSnippet();
        commentSnippet.TextOriginal = commentToAdd;

        Comment topLevelComment = new Comment();
        topLevelComment.Snippet = commentSnippet;

        CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
        commentThreadSnippet.VideoId = videoID;
        commentThreadSnippet.TopLevelComment = topLevelComment;

        CommentThread commentThread = new CommentThread();
        commentThread.Snippet = commentThreadSnippet;

        var youtubeService = await NewYouTubeService();
        CommentThreadsResource.InsertRequest insertComment = youtubeService.CommentThreads.Insert(commentThread, "snippet");

        await insertComment.ExecuteAsync();
    }
  • You basically just wrapped Nikola's answer in async / await. At least accept his answer and delete your other "answer". – Millie Smith Jan 02 '16 at 21:18