【问题标题】:An unhandled exception of type 'System.IndexOutOfRangeException' occurred. Additional information: Index was outside the bounds of the array发生了“System.IndexOutOfRangeException”类型的未处理异常。附加信息:索引超出了数组的范围
【发布时间】:2013-05-23 10:37:48
【问题描述】:

我的代码显示未处理的 ArrayIndexOutOfRangeException 异常。

    public static DateTime storeTime(String date)
    {
        string[] dateSplit;
        string[] timeSplit;
        DateTime returnValue = new DateTime();

        if (date == "")
            return returnValue;

        dateSplit = date.Split(new Char[] { 'T' });
        timeSplit = dateSplit[1].Split(new Char[] { '+' }); // Exception occurs here.
        timeSplit[0] = timeSplit[0].Substring(0, timeSplit[0].Length - 1);
        returnValue = DateTime.ParseExact(dateSplit[0] + " " + timeSplit[0], "yyyy-MM-dd HH:mm:ss",null);

        return returnValue;

    }

【问题讨论】:

  • 听起来 dateSplit[1] 上没有元素...您将什么参数(日期)推入您的方法。请问您应该调试一下,dateSplit 中存储的内容,以解决您的问题...
  • 你能粘贴DateTime类吗?
  • 将 Rohit 的编辑从 [java] 更改为 [c#]...如果我这样做了,请纠正我。错了

标签: c# .net indexoutofboundsexception


【解决方案1】:

您输入的参数date 不包含任何字符 T,因此您的拆分仅产生一个元素。为此尝试访问 dateSplit[1] (第二个元素)失败。您给出的索引 1 超出范围,只允许 0,因为只有一个元素。

请注意,这是一个非常基本的错误,您可能已被否决,因为您应该能够自己检测到这一点。下次,按 F5 调试并检查崩溃时的所有变量,以便您自己解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 2014-05-17
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多