在C#Timer TweetSharp中未处理NullReferenceException [英] NullReferenceException was unhandled in C# Timer TweetSharp

查看:97
本文介绍了在C#Timer TweetSharp中未处理NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Twitter项目,该项目使用户可以使用C#自动将其推到上述推文.我在C#中使用TweetSharp.问题是,当我尝试使用计时器刷新显示上述推文的form_load时,我得到NullReferenceException是未处理的错误.我尝试将时间间隔从20秒更改为40秒,以防万一问题出在时间间隔上,但问题仍然存在,因此我不认为问题出在时间间隔上.如果有人可以帮助我解决这个问题,我将不胜感激.

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.

以下是何时刷新Form1_Load:

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

        }

再次感谢您.

推荐答案

上周我恰好遇到了这个问题.我强烈怀疑这与访问令牌到期或其他错误有关:我经常看到这种情况是在程序运行5分钟(每20秒轮询一次)之后发生的.

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).

我添加了一些代码来检查返回值是否为null,在这种情况下只记录一条诊断消息,否则将其与空列表一样对待.我一次看到它从这种情况中恢复了,但是通常一旦它开始返回null,它就会继续这样做.我怀疑(但尚未检查源代码),发现某个地方的代码正在捕获异常,并且仅在出现任何错误时返回null(如果确实是原因,这显然很烦人)

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).

这当然不能帮助您立即解决问题-除了建议我使用相同的棘手解决方法(将null处理为空并记录),但这至少表明这不是什么您做错了-除非我做错了同样的事情:)

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 :)

现在,我浏览了TweetSharp代码,看起来TweetSharp和Hammock之间的交互有效地掩盖了异常,这真是令人遗憾.因此,返回值null不一定表示访问令牌问题-只是错误" :(调整TweetSharp代码库以适当地抛出异常可能并不难...或者它也许值得寻找替代的API.

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.

这篇关于在C#Timer TweetSharp中未处理NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆