1

我正在尝试将社交媒体与 siebel

我收到一个错误

“索引超出了数组的范围”

我无法修复错误

代码:

    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 = 100 });
        string[] twt_id = new string[100];
        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);
                        twtid = Convert.ToInt64(tweet.Id).ToString();
                        i = i + 1;
                        twt_id[i] = twtid;
                        increment = increment + 1;//Here I get an error
                    }

                    if (increment == 1)
                    {
                        //Reply related reply information
                        i = 0;
                        tweet_id = tweet.Id.ToString();
                        DbCommand = new OleDbCommand("select cust.first_name from mw_response resp, mw_customer cust where resp.response_id = '" + twt_id[i] + "' and resp.post_id is null and resp.customer_id= cust.customer_id", DbConnection);
                        OleDbDataReader DbReader = DbCommand.ExecuteReader();
                        while (DbReader.Read())
                        {
                            screenname = DbReader[0].ToString();
                            DbCommand = new OleDbCommand("select post_id,prod_id from mw_post where post_id = '" + id + "'", DbConnection);
                            OleDbDataReader DbReader0 = DbCommand.ExecuteReader();
                            while (DbReader0.Read())
                            {
                                post_id = DbReader0[0].ToString();
                                prod_id = DbReader0[1].ToString();

                                DbCommand = new OleDbCommand("update mw_response set prod_id = '" + prod_id + "',post_id = '" + post_id + "' where response_id = '" + twt_id[i] + "'", DbConnection);
                                DbCommand.ExecuteNonQuery();

                                //Invoking Siebel Web Service
                                if (screenname != "IIPL_LTD")
                                {
                                    createComment(twt_id[i]);
                                }
                            }
                            DbReader0.Close();
                        }
                        DbReader.Close();
                        increment = 0;
                        i = 0;
                    }
                    else
                    {
                        i = 0;
                        while (increment > 0)
                        {
                            //Reply related reply information 
                            DbCommand = new OleDbCommand("select cust.first_name from mw_response resp, mw_customer cust where resp.response_id = '" + twt_id[i] + "' and resp.post_id is null and resp.customer_id= cust.customer_id", DbConnection);
                            OleDbDataReader DbReader = DbCommand.ExecuteReader();
                            while (DbReader.Read())
                            {
                                screenname = DbReader[0].ToString();
                                DbCommand = new OleDbCommand("select post_id,prod_id from mw_post where post_id = '" + id + "'", DbConnection);
                                OleDbDataReader DbReader0 = DbCommand.ExecuteReader();
                                while (DbReader0.Read())
                                {
                                    post_id = DbReader0[0].ToString();
                                    prod_id = DbReader0[1].ToString();

                                    DbCommand = new OleDbCommand("update mw_response set prod_id = '" + prod_id + "',post_id = '" + post_id + "' where response_id = '" + twt_id[i] + "'", DbConnection);
                                    DbCommand.ExecuteNonQuery();

                                    //Invoking Siebel Web Service
                                    if (screenname != "IIPL_LTD")
                                    {
                                        createComment(twt_id[i]);
                                    }
                                }
                                DbReader0.Close();
                            }
                            DbReader.Close();
                            increment = increment - 1;
                            i = i + 1;
                        }
                        i = 0;
                    }
                }
            }
        }
        DbConnection.Close();
    }

这是程序被终止的地方 Index 超出了数组的范围

 if(i == 0)
 {
    twt_id[i] = tweet.Id.ToString();
 }
 id = Convert.ToInt64(replyid);
 twtid = Convert.ToInt64(tweet.Id).ToString();
 i = i + 1;
 twt_id[i] = twtid;
 increment = increment + 1;

有任何想法吗?提前致谢。

4

1 回答 1

1

如果replyid不是空字符串,您的代码将进入 while 循环,并且永远不会退出它。当i增加到 100 时,代码然后尝试访问twt_id[100]导致此异常的访问。有人会认为这意味着您打算replyid在循环体结束之前进行修改,但还没有解决。

当然,单步执行您的代码 - 或检查i异常点的值并推断原因- 会揭示这个问题。

于 2013-08-13T06:42:41.317 回答