NullReferenceException was unhandled in C# Timer TweetSharp

I am working on a twitter project that lets user to auto-tweet to the mentioned tweets using C#. I am using TweetSharp with C#. The problem is, when I try to refresh the form_load that shows the mentioned tweets using timer, I get NullReferenceException was unhandled error. I tried change the time interval from 20seconds to 40seconds just in case if the problem is with the time interval, but the problem still existed, hence I don't think the problem is with the time interval. If anyone can help me with this problem I will really appreciate.

Below is when to refresh the Form1_Load:

        var timer = new Timer();
        timer.Tick += new EventHandler(Form1_Load);
        timer.Interval = 20000; //40 seconds
        timer.Start();

This is where I get the error:

    private void Form1_Load(object sender, EventArgs e) // form load bolumu
    {

        listView1.Items.Clear();
        tweetid.Items.Clear();
        userid.Items.Clear();
        username.Items.Clear();

        var received= service.ListTweetsMentioningMe(new ListTweetsMentioningMeOptions { Count = 15 });

        **foreach (var tweet in received)** --> This is where I get the error NullException
        {
            ListViewItem lvi = new ListViewItem(tweet.Text);
            listView1.Items.Add(lvi);
            lvi.SubItems.Add("@" + tweet.User.ScreenName.ToString());
            lvi.SubItems.Add(tweet.CreatedDate.ToString());

            userid.Items.Add(tweet.User.Id.ToString());
            tweetid.Items.Add(tweet.Id.ToString());

        }

Thank you again.

Jon Skeet
people
quotationmark

I've come across exactly this problem in the last week. I strongly suspect that it is related to access tokens expiring or other errors: I've regularly seen this occur after my program has been active for 5 minutes (of polling every 20 seconds).

I've added some code to check whether the return value is null, and just log a diagnostic message in that case, otherwise treating it the same way as an empty list. I have once seen it recover from this condition, but usually once it's started returning null it just continues doing so. I suspect (but haven't checked the source code) that somewhere there's code which is catching an exception and just returning null on any error (which is distinctly annoying, if it really is the cause).

This doesn't help you resolve the problem immediately of course - other than suggesting the same icky workaround I've used (treating null as empty and logging) but it's at least an indication that this isn't anything you're doing wrong - unless I'm doing the same thing wrong :)

EDIT: I've now looked through the TweetSharp code, and it looks like the interaction between TweetSharp and Hammock effectively masks exceptions, which is a great pity. So a return value of null doesn't necessarily indicate an access token issue - it's just "an error" :( It probably wouldn't be too hard to tweak the TweetSharp code base to throw exceptions appropriately instead... or it may be worth looking for an alternative API.

people

See more on this question at Stackoverflow