简体   繁体   中英

Index was outside the bounds of the array when using foreach

I'm trying to integrate social media with siebel

I get an error

"Index was outside bounds of the array"

I am unable to fix the error

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

this is where the program was terminated by Index was outside bounds of the array

 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;

Any ideas? Thanks in advance.

If replyid is not an empty string, your code enters the while loop, and never exits it . When i is incremented to 100, the code then attempts to access twt_id[100] which causes this exception. One would presume this to mean you intended to modify replyid before the end of the loop body but hadn't gotten around to it.

Surely stepping through your code - or examining the value of i at the point of exception and extrapolating why - would have revealed this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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