I have created a program for my ASP.NET class to create a class name and number and then enter in ten student names with student ID. So my web page will prompt the user to enter these 4 information pieces separating them into the MyClass ( the course name and number) and the Student class ( where it holds the student name and ID). I am terribly stuck on the NullReferenceException error and don’t know what to do. If anyone here could give me some pointers and direction so I can pick myself up from this assignment. One thing is certain, I am not allowed to enter any new methods and variables. I am only able to use the current ones I have initialized.
- The Student[] array is the array that holds my Student class with the name and id of students.
- The user first will create the class number and name in which to add students(with id and name) to it afterwards.
- Only 1 class name and number(they go together) is allowed and only 10 students is the maximum.
I am getting the null exception on my ToString() method in MyClass where classNumberAndName += ((Student)students[i]).ToString();
Here’s my code with no syntax errors.
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;
}
}
And here is the .aspx.cs file for the web code.
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;
}
}
}
The problem is in this loop:
You are initialising students with
students = new Student[10]sostudents.Lengthwill be10even if you have inserted less than10students. The values you haven’t filled in arenull, so you end up doingnull.ToString, hence theNullReferenceException.I believe you should be using
numberOfStudentsinstead: