0

Here is my code:

var reddit = new Reddit();
var authenticated = false;
try
{
    var user = reddit.LogIn("uname", "password");
    authenticated = reddit.User != null;
}
catch (AuthenticationException)
{
    Console.WriteLine("Incorrect login.");
    authenticated = false;
}
//RedditSharp.Things.Thing.Parse.
var subreddit = reddit.RSlashAll;
var allPost = subreddit.Search("domain").Take(3);
foreach(var p in allPost)
{
    Console.WriteLine(p.Comments);
    var comment = p.Comment("aaaaaaaaaaaaaaaaaa");
    comment.Distinguish(RedditSharp.Things.VotableThing.DistinguishType.Moderator);
}

I am getting an unhandled exception of type

An unhandled exception of type 'System.NullReferenceException'occurred in RedditSharp.dll Additional information: Object reference not set to an instance of an object.

occurred in

var comment = p.Comment("aaaaaaaaaaaaaaaaaa");
Hakunamatata
  • 1,275
  • 13
  • 18
  • If `p` is null then `Console.WriteLine(p.Comments);` must throw exception before `var comment = p.Comment("aaaaaaaaaaaaaaaaaa");` – NASSER Sep 21 '15 at 03:25
  • The error says `occurred in RedditSharp.dll`, are you sure you have the line right? Maybe it's actually thrown from [somewhere around here](https://github.com/SirCmpwn/RedditSharp/blob/master/RedditSharp/Things/Post.cs#L164). – 31eee384 Sep 21 '15 at 04:02

1 Answers1

1

The error message references the wrong line. Check that p.comments is not null before asking for the value.

foreach(var p in allPost)
{
  if(p.comments != null)
  {
    Console.WriteLine(p.Comments);
    var comment = p.Comment("aaaaaaaaaaaaaaaaaa");
  }    
}
g williams
  • 184
  • 1
  • 7