Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginner headaches: NullReferenceException from array on ASP.NET webfrom
    primarykey
    data
    text
    <p>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 <strong>MyClass ( the course name and number)</strong> and the <strong>Student class ( where it holds the student name and ID)</strong>. 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 <strong>not allowed</strong> to enter any new methods and variables. I am only able to use the current ones I have initialized.</p> <ul> <li>The Student[] array is the array that holds my Student class with the name and id of students. </li> <li>The user first will create the class number and name in which to add students(with id and name) to it afterwards.</li> <li>Only 1 class name and number(they go together) is allowed and only 10 students is the maximum.</li> </ul> <p><em><strong>I am getting the null exception on my ToString() method in MyClass where classNumberAndName += ((Student)students[i]).ToString();</em></strong></p> <p>Here's my code with no syntax errors. </p> <pre><code>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 &lt; 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 &lt; 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 &lt; 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; } } </code></pre> <p>And here is the .aspx.cs file for the web code. </p> <pre><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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload