【问题标题】:c# - Object reference not set to an instancec# - 对象引用未设置为实例
【发布时间】:2013-08-14 09:39:37
【问题描述】:

我正在尝试从 twitter 获取推文

但我得到一个错误

对象引用未设置为实例

代码:

    public void Call_Tweet_Update()
    {
        var service = new TwitterService(Consumer_Key, Consumer_Secret);
        service.AuthenticateWith(Access_Token, AccessToken_Secret);

        var tweets = service.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions { Count = 200 });
        string[] twt_id = new string[50];
        long id = 0;
        int i = 0;
        int increment = 0;
        string twtid;
        string screenname;

        foreach (var tweet in tweets)
        {
            if (tweet.InReplyToStatusId.ToString() != "")
            {
                if ((tweet.User.ScreenName == "IIPL_LTD") || (tweet.Text.StartsWith("@IIPL_LTD")))
                {
                    string replyid = tweet.InReplyToStatusId.ToString();

                    while (replyid != "")
                    {
                        if (i == 0)
                        {
                            twt_id[i] = tweet.Id.ToString();
                        }
                        id = Convert.ToInt64(replyid);
                        var twt = service.GetTweet(new GetTweetOptions { Id = id });
                        //twtid = Convert.ToInt64(tweet.Id).ToString();
                        twtid = Convert.ToInt64(twt.Id).ToString();
                        //abc = twtid;
                        i = i + 1;
                        twt_id[i] = twtid;
                        replyid = twt.InReplyToStatusId.ToString();
                       // replyid = tweet.InReplyToStatusId.ToString();
                        // replyid = "";
                        increment = increment + 1;
                    }
                  }
                }

这是由未设置为实例的对象引用终止的代码

获取 id 为 38811592704 但以下行获取空值

var twt = service.GetTweet(new GetTweetOptions { Id = id });

   while (replyid != "")
   {
       if (i == 0)
       {
          twt_id[i] = tweet.Id.ToString();
       }
       id = Convert.ToInt64(replyid);
       var twt = service.GetTweet(new GetTweetOptions { Id = id });
       twtid = Convert.ToInt64(twt.Id).ToString();
    }

有什么想法吗?提前致谢。

【问题讨论】:

  • 检查replyid的值是否为null..
  • 在 Debug 中运行它并检查哪个值实际上为空。我想我们必须假设 id 为空,因为如果以前使用过该服务,则不应该是...检查实际在 replyid 中的内容
  • 我可以得到replyid

标签: c# asp.net twitter


【解决方案1】:

这里唯一会引起你问题的是你的类构造函数

new GetTweetOptions

给你例外。去这个类的构造函数看看是什么造成的错误

【讨论】:

    【解决方案2】:

    调试您的代码检查您收到此错误的对象 然后尝试通过使用 NEW 关键字创建该对象的新实例来解决此错误

    例如假设 var twt = service.GetTweet(new GetTweetOptions { Id = id });

    对象中的服务,然后您将在使用该对象之前创建一个新实例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多