Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:45:52+00:00 2026-06-11T13:45:52+00:00

I have created a program for my ASP.NET class to create a class name

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:45:53+00:00Added an answer on June 11, 2026 at 1:45 pm

    The problem is in this loop:

    for(int i = 0; i < students.Length; i++)
    {
        classNumberAndName += ((Student)students[i]).ToString();
    }
    

    You are initialising students with students = new Student[10] so students.Length will be 10 even if you have inserted less than 10 students. The values you haven’t filled in are null, so you end up doing null.ToString, hence the NullReferenceException.

    I believe you should be using numberOfStudents instead:

    for(int i = 0; i < numberOfStudents; i++)
    {
        classNumberAndName += ((Student)students[i]).ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET (C#) web page which utilizes a VB class library. The
I have a program written in asp.net with lucene.net. At first I create an
I have program with asp.net c# but now i need to create an application
I have created a non-form c# program that uses the NotifyIcon class. The text
We have an asp.net application that is able to create .air files. To do
I am writing a web app in asp.net. I have an aspx page that
I have developed an ASP.NET web application in Visual Studio 2008. I have an
I have a asp.net program, and I need to send a lot of emails.
I have an Asp.Net web site (there is no .csproj file). This web site's
I create new ASP.NET web application that use SMTP to send message. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.