简体   繁体   English

在C#Timer TweetSharp中未处理NullReferenceException

[英]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#. 我正在开发一个Twitter项目,该项目允许用户使用C#自动将其推到上述推文。 I am using TweetSharp with C#. 我在C#中使用TweetSharp。 The problem is, when I try to refresh the form_load that shows the mentioned tweets using timer, I get NullReferenceException was unhandled error. 问题是,当我尝试使用计时器刷新显示上述推文的form_load时,我得到NullReferenceException是未处理的错误。 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. 我尝试将时间间隔从20秒更改为40秒,以防万一问题出在时间间隔上,但问题仍然存在,因此我不认为问题出在时间间隔上。 If anyone can help me with this problem I will really appreciate. 如果有人可以帮助我解决这个问题,我将不胜感激。

Below is when to refresh the Form1_Load: 下面是刷新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. 再次感谢你。

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). 我强烈怀疑这与访问令牌到期或其他错误有关:我经常看到这种情况是在程序运行5分钟(每20秒轮询一次)之后发生的。

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. 我添加了一些代码来检查返回值是否为null ,在这种情况下只记录一条诊断消息,否则将其视为空列表。 I have once seen it recover from this condition, but usually once it's started returning null it just continues doing so. 曾经看到它从这种情况中恢复过来,但是通常一旦它开始返回null它就会继续这样做。 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 :) 当然,这并不能帮助您立即解决问题-除了建议我使用过同样的变通方法(将null处理为空并记录日志)外,但这至少表明这不是做错了什么-除非我做错了同样的事情:)

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. 编辑:我现在已经浏览了TweetSharp代码,并且看起来TweetSharp和Hammock之间的交互有效地掩盖了异常,这是非常遗憾的。 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. 因此,返回值null不一定表示访问令牌问题-只是“错误” :(调整TweetSharp代码库以适当地抛出异常可能不是太难了……或者可能是值得寻找替代的API。

Check for null values before looping on the result: 在循环结果之前检查空值:

var gelen = ts.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions() { Count = 1 });
if (gelen != null) {
    foreach (var tweet in gelen)
    {
        //    s = tweet.Text;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM