C# 尝试从C中的文本文件创建对象时发生IndexOutOfRangeException# 我目前正试图获取一个“被”分隔的文本文件,并从包含在其中的数据创建对象。例如: Name|Address|City|State|Zip|Birthday|ID|Etc. Name2|Address2|City2|State2|Zip2|Birthday2|ID2|Etc.
然后,新创建的对象被添加到所述对象的列表中,程序使用.Peek()通过while循环移动到文件的下一行(以确保我不会超过文件的结尾) 然而,当它开始创建第二个对象(更具体地说,第二个对象的第二个字段)时,它抛出了一个索引超出范围异常,我一辈子都不知道为什么。谢谢所有可能读到这篇文章的人C# 尝试从C中的文本文件创建对象时发生IndexOutOfRangeException# 我目前正试图获取一个“被”分隔的文本文件,并从包含在其中的数据创建对象。例如: Name|Address|City|State|Zip|Birthday|ID|Etc. Name2|Address2|City2|State2|Zip2|Birthday2|ID2|Etc.,c#,list,object,text,C#,List,Object,Text,然后,新创建的对象被添加到所述对象的列表中,程序使用.Peek()通过while循环移动到文件的下一行(以确保我不会超过文件的结尾) 然而,当它开始创建第二个对象(更具体地说,第二个对象的第二个字段)时,它抛出了一个索引超出范围异常,我一辈子都不知道为什么。谢谢所有可能读到这篇文章的人 StreamReader textIn = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
StreamReader textIn = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
List<Student> students = new List<Student>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
MessageBox.Show(row);
string [] fields = row.Split('|');
Student temp = new Student();
try
{
temp.name = fields[0];
temp.address = fields[1];
temp.city = fields[2];
temp.state = fields[3];
temp.zipCode = Convert.ToInt32(fields[4]);
temp.birthdate = fields[5];
temp.studentID = Convert.ToInt32(fields[6]);
temp.sGPA = Convert.ToDouble(fields[7]);
}
catch
{
MessageBox.Show("IndexOutOfRangeException caught");
}
students.Add(temp);
}
textIn.Close();
StreamReader textIn=newstreamreader(newfilestream(path,FileMode.OpenOrCreate,FileAccess.Read));
列出学生=新建列表();
while(textIn.Peek()!=-1)
{
string row=textIn.ReadLine();
MessageBox.Show(行);
string[]fields=row.Split(“|”);
学生临时工=新学生();
尝试
{
temp.name=字段[0];
临时地址=字段[1];
临时城市=字段[2];
温度状态=字段[3];
temp.zipCode=Convert.ToInt32(字段[4]);
临时出生日期=字段[5];
temp.studentID=转换为32(字段[6]);
temp.sGPA=转换为双重(字段[7]);
}
抓住
{
MessageBox.Show(“IndexOutOfRangeException捕获”);
}
学生。添加(临时);
}
textIn.Close();
首先,您无法确定当前catch块是否存在IndexOutfrange异常
catch
{
MessageBox.Show("IndexOutOfRangeException caught");
}
它可以是任何东西,在解析为double时可能是异常。您可以将捕捉块修改为:
catch(IndexOutOfRangeException ex)
{
MessageBox.Show(ex.Message);
}
另外,如果您要访问字段[7]
,那么最好检查数组的长度,以确保数组中至少有8个元素
if(fileds.Length >=8)
{
temp.name = fields[0];
....
要捕获双重解析期间可能发生的FormatException
,您可以为以下内容添加额外的catch块:
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
在给定的数据中,如果每行至少有八列,则不会获得范围异常的索引,但是
解析4、6、7处的项将失败,因为它们是非
数字,并将非数字值转换为int和double引发异常
temp.zipCode = Convert.ToInt32(fields[4]);
temp.studentID = Convert.ToInt32(fields[6]);
temp.sGPA = Convert.ToDouble(fields[7]);
您需要更改catch块以了解异常的原因
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
检查你是否有所有的8个场在一条线上李>
如果没有,则显示消息李>
获取实际异常并显示其消息以查看实际问题描述李>
使用并确保所有数值都有效
也可以在(!textIn.EndOfStream)
时使用
如果数据在每一行上,ReadAllLines
可能会工作得更好:
List<Student> students = new List<Student>();
using (FileStream textIn = new FileStream(path, FileMode.Open, FileAccess.Read))
{
foreach (string line in File.ReadAllLines(path))
{
MessageBox.Show(line);
string[] fields = line.Split('|');
Student temp = new Student();
try
{
temp.name = fields[0];
temp.address = fields[1];
temp.city = fields[2];
temp.state = fields[3];
temp.zipCode = Convert.ToInt32(fields[4]);
temp.birthdate = fields[5];
temp.studentID = Convert.ToInt32(fields[6]);
temp.sGPA = Convert.ToDouble(fields[7]);
}
catch
{
MessageBox.Show(string.Format("IndexOutOfRangeException caught, Split Result:", string.Join(", ", fields.ToArray())));
}
students.Add(temp);
}
}
List students=newlist();
使用(FileStream textIn=newfilestream(路径,FileMode.Open,FileAccess.Read))
{
foreach(文件中的字符串行.ReadAllLines(路径))
{
MessageBox.Show(行);
string[]fields=line.Split(“|”);
学生临时工=新学生();
尝试
{
temp.name=字段[0];
临时地址=字段[1];
临时城市=字段[2];
温度状态=字段[3];
temp.zipCode=Convert.ToInt32(字段[4]);
临时出生日期=字段[5];
temp.studentID=转换为32(字段[6]);
temp.sGPA=转换为双重(字段[7]);
}
抓住
{
Show(string.Format(“IndexOutOfRangeException捕获,拆分结果:”,string.Join(“,”,fields.ToArray()));
}
学生。添加(临时);
}
}
您确定它实际上是一个索引自动异常
,因为您正在捕获所有异常?此外,您可能希望在处理每一行之前尝试打印出来以进行调试。删除try-catch后,我得到一个错误:应用程序中发生了未处理的异常(等等)索引超出了数组的边界。异常文本:System.IndexOutOfRangeException:索引超出了数组的边界。
List<Student> students = new List<Student>();
using (FileStream textIn = new FileStream(path, FileMode.Open, FileAccess.Read))
{
foreach (string line in File.ReadAllLines(path))
{
MessageBox.Show(line);
string[] fields = line.Split('|');
Student temp = new Student();
try
{
temp.name = fields[0];
temp.address = fields[1];
temp.city = fields[2];
temp.state = fields[3];
temp.zipCode = Convert.ToInt32(fields[4]);
temp.birthdate = fields[5];
temp.studentID = Convert.ToInt32(fields[6]);
temp.sGPA = Convert.ToDouble(fields[7]);
}
catch
{
MessageBox.Show(string.Format("IndexOutOfRangeException caught, Split Result:", string.Join(", ", fields.ToArray())));
}
students.Add(temp);
}
}