我为我的 ASP.NET 班级创建了一个程序来创建班级名称和编号,然后输入十个学生姓名和学生 ID。所以我的网页会提示用户输入这 4 条信息,将它们分为MyClass(课程名称和编号)和Student 班级(其中包含学生姓名和 ID)。我非常卡在 NullReferenceException 错误上,不知道该怎么做。如果这里有人可以给我一些指示和方向,那么我可以从这项任务中振作起来。有一件事是肯定的,我不允许输入任何新的方法和变量。我只能使用我已经初始化的当前那些。
- Student[] 数组是保存我的 Student 类的数组,其中包含学生的姓名和 id。
- 用户首先将创建班级编号和姓名,然后在其中添加学生(带有 ID 和姓名)。
- 只允许 1 个班级名称和编号(他们一起去),最多只能有 10 名学生。
我在 MyClass 中的 ToString() 方法中遇到空异常,其中 classNumberAndName += ((Student)students[i]).ToString();
这是我的代码,没有语法错误。
public class MyClass
{
private string courseNumber;
private string courseName;
private int numberOfStudents;
private Student[] students;
public MyClass(string CourseNumber , string CourseName, Student[] Student)
{
students = new Student[Student.Length];
courseNumber = CourseNumber;
courseName = CourseName;
for (int i = 0; i < Student.Length; i++)
{
Student tmpArrayStudent = new Student(((Student)students[i]).ToString(),((Student)students[i]).ToString());
tmpArrayStudent = students[i];
}
}
public MyClass(string CourseNumber, string CourseName)
{
courseNumber = CourseNumber;
courseName = CourseName;
students = new Student[10];
}
public void addAStudent(Student student)
{//possible to create temp array?
Student[] students = new Student[10];
if(numberOfStudents < students.Length)
{
students[numberOfStudents] = student;
numberOfStudents++;
}
else
{
throw new Exception("Amount of Students Exceeded, no more than 10");
}
}
public string getClassNumberAndName()
{
return "Course Number: " + courseNumber + " " + " Course Name: " + courseName;
}
public override string ToString()
{
string classNumberAndName = getClassNumberAndName();
for(int i = 0; i < students.Length; i++)
{
classNumberAndName += ((Student)students[i]).ToString();
}
classNumberAndName += "Students Registered: " + numberOfStudents.ToString();
return classNumberAndName;
}
}
public class Student
{
private string idNumber;
private string name;
public Student(string ID, string Name)
{
idNumber = ID;
name = Name;
}
public override string ToString()
{
return "Student ID: " + idNumber + " " + " Student Name " + name;
}
}
这是 Web 代码的 .aspx.cs 文件。
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
pnlStudent.Visible = false;//use panels to disable the ability to enter new course information
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MyClass course = new MyClass(txtCnum.Text, txtCname.Text);
Session.Add("Course", course);
((MyClass)Session["Course"]).addAStudent(new Student(txtCnum.Text, txtCname.Text));//since we entered to Student, course information or student information?
txtAnswers.Text += course.ToString();
pnlStudent.Visible = true;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Student student = new Student(txtID.Text, txtName.Text);
pnlCourse.Visible = false;
try
{
((MyClass)Session["Course"]).addAStudent(new Student(txtID.Text, txtName.Text));
txtAnswers.Text += Session["Course"].ToString();
}
catch(Exception ex)
{
txtAnswers.Text += "Amount of students are at maximum." + ex.Message;
}
}
}