【问题标题】:Index outside bounds of array数组边界外的索引
【发布时间】:2013-08-14 05:35:45
【问题描述】:

我正在尝试从 facebook 获取所有数据

但我得到一个错误

索引超出了数组的范围

代码:

 public void Comments(string cmtid)
    {
        var accessToken = "***********";
        var client = new FacebookClient(accessToken);

        clientfeed = client.Get(cmtid).ToString();

        JObject obj = JObject.Parse(clientfeed);

        JArray datas = (JArray)obj["data"];

        for (int i = 0; i < datas.Count; i++)
        {
            if (obj["data"][i]["message"] != null)
            {
                strPostComment = obj["data"][i]["message"].ToString();
                if (strPostComment.Contains("'"))
                {
                    strPostComment = strPostComment.Replace("'", "''");
                }
                strPostCommentId = obj["data"][i]["id"].ToString();
                strPostCommentNameId = obj["data"][i]["from"]["id"].ToString();
                strPostCommentName = obj["data"][i]["from"]["name"].ToString();
                splitCommentId = strPostCommentId.Split('_');
                strPostCommentdate = obj["data"][i]["created_time"].ToString();
                if (strPostCommentdate.Contains("T"))
                {
                    strPostCommentdate = strPostCommentdate.Replace("T", " ");
                }
                abccommenttime = strPostCommentdate.Substring(0, strPostCommentdate.ToString().Length - 5);

                if (strPostCommentName == "IIPL BANK")
                {
                    IIPLCustomer(strPostCommentNameId, abccommenttime);
                }
                else
                {
                    Customer(strPostCommentNameId);
                }

                if (datas.Count != 0)
                {
                    DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[2] + "'", DbConnection);
                    OleDbDataReader DbReader = DbCommand.ExecuteReader();

                    while (DbReader.Read())
                    {
                        count = DbReader[0].ToString();
                        cnt = Convert.ToInt32(count);

                        if ((cnt == 0) && (strPostComment != ""))
                        {
                            DbCommand = new OleDbCommand("insert into mw_response(post_id,response,response_id, resp_date,community) values('" + splitCommentId[1] + "','" + strPostComment + "','" + splitCommentId[2] + "', to_date('" + abccommenttime + "', 'yyyy-mm-dd hh24:mi:ss'),'" + fb_community + "')", DbConnection);
                            DbCommand.ExecuteNonQuery();

                            //update productid and customerid
                            DbCommand = new OleDbCommand("update mw_response set prod_id = (select prod_id from mw_post where post_id='" + splitCommentId[1] + "'),customer_id = (select customer_id from mw_customer where customer_id = '" + strPostCommentNameId + "') where response_id = '" + splitCommentId[2] + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();

                            if (strPostCommentName != "IIPL BANK")
                            {
                                //calling comment web service
                                createComment(splitCommentId[2]);
                            }
                        }
                    }
                    DbReader.Close();
                }
            }
        }
    }

这就是被 Indes 终止的代码超出了数组的范围

  if (datas.Count != 0)
  {
     DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[2] + "'", DbConnection);
     OleDbDataReader DbReader = DbCommand.ExecuteReader();
  }

【问题讨论】:

  • 使用前为什么不检查 splitCommentId[] 的大小?
  • if (datas.Count != 0) 意义较小。因为它不会进入循环for (int i = 0; i &lt; datas.Count; i++) 如果计数小于 0

标签: c# foreach


【解决方案1】:

您正在尝试访问 splitCommentId 中索引 2 处的数据,而不检查该处的数据是否确实存在于此处:

DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[2] + "'", DbConnection);

你可以这样做

if (datas.Count != 0 && splitCommentId.length >= 3)
{
     DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[2] + "'", DbConnection);
     OleDbDataReader DbReader = DbCommand.ExecuteReader();
}

【讨论】:

    【解决方案2】:

    在下面一行

    splitCommentId = strPostCommentId.Split('_');
    

    检查 splitCommentId 的长度。它将是 2。这意味着在下面的行中

    DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[2] + "'", DbConnection);
    

    splitCommentId[2] 将不存在。因此错误。如果你想访问第二个元素,那么就这样做吧

    DbCommand = new OleDbCommand("select count(response_id) from mw_response where response_id = '" + splitCommentId[1] + "'", DbConnection);
    

    您的代码 createComment(splitCommentId[2]); 也会抛出异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 2011-04-15
      相关资源
      最近更新 更多