0

I've searched through many threads and websites looking for this problem. So far, I haven't been able to find anything wrong with this code.

The "bad" code is this: request.AddComment(v, c);

Also, I don't know what a stack trace is.

Thanks for all your help in advance.

Here is my code:

string devkey = "1"; string username = "2"; string password = "3"; YouTubeRequestSettings a = new YouTubeRequestSettings("test", devkey, username, password); YouTubeRequest request = new YouTubeRequest(a); Uri uri = new Uri("b"); Video v = request.Retrieve<Video>(uri); Comment c = new Comment(); c.Content = "asdf"; request.AddComment(v, c); 
3
  • 5
    Please add the stack trace or at least the line that is having the problem Commented Jun 28, 2011 at 20:27
  • 2
    Most probanbly in this line Video v = request.Retrieve<Video>(uri); v stays null and on line request.AddComment(v, c); the exception is thrown. Can you check that? Commented Jun 28, 2011 at 20:28
  • 1
    Breakpoints in Visual-Studio. Commented Jun 28, 2011 at 20:35

2 Answers 2

3

The only way this snippet might throw a NullReferenceException is if request.Retrieve returned null and request.AddComment throws an exception if either parameter is null.

The solution is to test v:

Video v = request.Retrieve<Video>(uri); if(v != null) { Comment c = new Comment(); c.Content = "asdf"; request.AddComment(v, c); } else { // something went wrong when getting the video... } 
Sign up to request clarification or add additional context in comments.

Comments

0

Null check the objects that are being referenced. The video request definately should be checked. Code below does a video null check.

string devkey = "1"; string username = "2"; string password = "3"; YouTubeRequestSettings a = new YouTubeRequestSettings("test", devkey, username, password); YouTubeRequest request = new YouTubeRequest(a); Uri uri = new Uri("b"); Video v = request.Retrieve<Video>(uri); Comment c = new Comment(); c.Content = "asdf"; if (v!= null) { request.AddComment(v, c); } else { //Handle the null, try to get the video again, report to user, etc. } 

3 Comments

new cannot return null. Testing request is unnecessary.
My bad, Video should be checked. I've updated the code, Thx for pointing that out.
Thanks, so now I know that v returns null :/ Now all I need to do is try and get it to return something else.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.