【问题标题】:Index out of Bounds Error?索引越界错误?
【发布时间】:2015-03-22 19:10:08
【问题描述】:

所以我正在开发一个应用程序,该应用程序需要我遍历所有条目并更新提醒日期并匹配每个用户的序列键。出于某种原因,即使我没有遍历所有已知索引,我也会遇到越界错误

public void updateReminders(RichTextBox rtb, RichTextBox rtb2, DataTable d1)
    {
        for(int i = 0; i < ChildRoot.Nodes.Count; i++)
        {

            dr = d1.Rows[i];
            TreeNode tn = ChildRoot.Nodes[i];

            DateTime dtCal = Convert.ToDateTime(dr["NEXTCONTACT"]);
            DateTime dtCalTime = dtCal.AddDays(60);

            string clientName = tn.Nodes[0].Text;
            string clientNameTrimmed = clientName.Split(new string[] { "NAME: " }, StringSplitOptions.None).Last();

            if(dtCalTime <= DateTime.Today)
            {
                rtb.AppendText("Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal + Environment.NewLine);
                rtb2.AppendText("Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal + Environment.NewLine);
                tn.Nodes[4].Text = "NEXT CONTACT: " + DateTime.Today.ToLongDateString();

                dr = dts1.Rows[i];
                dr["NEXTCONTACT"] = DateTime.Today.ToLongDateString();
                dr["LASTCONTACT"] = DateTime.Today.ToLongDateString();
                dr.AcceptChanges();

                string currentPath = Directory.GetCurrentDirectory();

                if (!Directory.Exists(Path.Combine(currentPath, "AppData")))
                {
                    Directory.CreateDirectory(Path.Combine(currentPath, "AppData"));
                }

                try
                {
                    string savePath = string.Concat(Environment.CurrentDirectory, @"\AppData\ClientHistory_Calibration.ini");
                    string notePath = "Calibration Client: " + clientNameTrimmed + " is due for a call! " + dtCal;
                    string[] lines = { notePath };

                    System.IO.File.WriteAllLines(savePath, lines);
                    MessageBox.Show("Remind History has automatically been saved to 'Install directory/AppData/ClientHistory_Calibration.ini'", "Saved!");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error", e.ToString());
                }
            }
        }
    }

异常信息:

System.IndexOutOfRangeException: There is no row at position 3.
   at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
   at System.Data.DataRowCollection.get_Item(Int32 index)
   at ClientDataBase.Code.ClientTreeNodeView.updateReminders(RichTextBox rtb, RichTextBox rtb2, DataTable d1) in c:\Users\AdamSMI\Desktop\DatabaseTest\ClientDataBase\ClientDataBase\Code\ClientTreeNodeView.cs:line 541
   at ClientDataBase.Form1.Form1_Load(Object sender, EventArgs e) in c:\Users\AdamSMI\Desktop\DatabaseTest\ClientDataBase\ClientDataBase\Code\Form1.cs:line 57

【问题讨论】:

  • 为什么i &lt; ChildRoot.Nodes.Countd1.Rows[i]; 的有效守卫?
  • 你知道越界错误发生在哪一行吗?是否抛出 IndexOutOfRangeException?
  • 使用调试器或至少在堆栈跟踪中发布异常信息。
  • 你不能使用调试器告诉我们异常发生在哪一行吗?
  • 不要在 cmets 中添加信息,使用 edit 链接改进您的问题。然后您也可以正确格式化它。

标签: c# database loops indexing


【解决方案1】:

请注意,当您检查 ChildRoot.Nodes 中的计数时,您确实没有检查 Rows 中 dtdtsl 的数量 - 根据特别提到的错误消息判断row at position 3,其中之一就是罪魁祸首。确保您正在检查该行是否存在 - 如果不存在,那么在应用程序的某个位置保持这些行同步可能会出现问题,或者您可能需要在代码执行时添加它,而不是检索它。

【讨论】:

  • 感谢 Aravol 的正确回复。由于您的解决方案,我解决了这个问题。
猜你喜欢
  • 2014-06-06
  • 2013-10-13
  • 2015-01-10
  • 2014-01-27
  • 2017-03-23
  • 2020-04-23
  • 2011-12-13
相关资源
最近更新 更多