我在制作一个简单的程序时非常擅长编码并陷入困境。我想要做的是从.txt文件中读取数据并将其添加到类数组中,所以当我输出studClasses [0] .Days [0] .Lesson [1]时,它会显示"数学&# 34;等等。 问题是,我不知道如何解决问题,排在第一位[a] .Days [i] .AddLesson(line);"
static void Main(string[] args)
{
int CMax = 50;
StudentClass[] studClasses = new StudentClass[CMax];
ReadData(file, studClasses);
}
public class StudentClass
{
public const int CMax = 50;
public Day[] Days { get; set; }
public string ClassName { get; set; }
public StudentClass()
{
ClassName = "";
Day[] Days = new Day[CMax];
}
public void AddClass(string cls)
{
ClassName = cls;
}
}
public class Day
{
public const int CMax = 100;
public string[] Lessons { get; set; }
public int lessonNumber { get; set; }
public Day(string[] less)
{
Lessons = less;
}
public void AddLesson(string lesson)
{
Lessons[lessonNumber] = lesson;
lessonNumber++;
}
}
private static void ReadData(string file, StudentClass[] studClasses)
{
using (StreamReader reader = new StreamReader(@file))
{
int a = 0;
string line = null;
while (null != (line = reader.ReadLine()))
{
for (int i = 0; i < 5; i++) // 5 days Monday-Friday
{
for (int j = 0; j < 7; j++) // 7 lessons each day
{
line = reader.ReadLine();
studClasses[a].Days[i].AddLesson(line);
}
}
a++;
}
}
}
提前感谢您的帮助! 编辑:错误代码:未处理的类型&#39; System.NullReferenceException&#39;发生在ConsoleApplication1.exe中 附加信息:未将对象引用设置为对象的实例 EDIT2:添加了对ReadData的调用。