5

I'm trying to get tweets from twitter

But I get an error

Object reference not set to an instance

Code:

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

this is what the code terminated by Object reference not set to an instance

Getting id as 38811592704 but the below line getting null value

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

Any ideas? Thanks in advance.

Community
  • 1
  • 1
user2500094
  • 883
  • 6
  • 21
  • 41
  • 1
    check the value of replyid if its null.. – iJade Aug 14 '13 at 09:43
  • 1
    Run it in Debug and check which value is actually null. I guess we have to assume that the id is null, since the service shouldn't be if it was previously used...Check what is actually within replyid – Daniel Dawes Aug 14 '13 at 09:44
  • I can get the replyid – user2500094 Aug 14 '13 at 09:47
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – nvoigt Jan 30 '14 at 09:58

2 Answers2

1

The only thing that ca cause you problem here is your class constructor

new GetTweetOptions

is giving you exception. Go to the constructor of this class and see what is creating error

Ehsan
  • 28,801
  • 6
  • 51
  • 61
1

Debug your code Check at what Object you are getting this error then try to resolve this error by using NEW key word to make a new instance of that object

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

Service in an object then u will have make a new instanse before using that

vikzzz
  • 43
  • 1
  • 1
  • 5