类型为'System.NullReferenceException'的未处理异常

本文关键字:未处理 异常 NullReferenceException 类型 System | 更新日期: 2023-09-27 18:13:35

下面是我的代码:

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);
}

我得到了一个类型为

的未处理异常

类型为"System"的未处理异常。在RedditSharp.dll中发生了NullReferenceException的实例的对象引用对象。

发生

var comment = p.Comment("aaaaaaaaaaaaaaaaaa");

类型为'System.NullReferenceException'的未处理异常

错误消息引用了错误的行。在请求该值之前,检查p.p ecomments是否为空。

foreach(var p in allPost)
{
  if(p.comments != null)
  {
    Console.WriteLine(p.Comments);
    var comment = p.Comment("aaaaaaaaaaaaaaaaaa");
  }    
}