0

I'm new to C# and I tried making a "Student" class that takes the student name and few other things. I have a problem at taking student name as I get an exception when typing the name as input.

In declaration,

public class Student
{
     public string studentName;// this one
     public long studentID;
     public int score1;
     ...etc
}

then I have inside Main:

Student[] student = new Student[N];
// the N is determined by a previous block    of code.
for (int i = 0; i < N; i++)
{
    check = false; // ignore this one.
    Console.WriteLine("Student {0}", i + 1);
    Console.Write("\t \t Name: ");
    string input = Console.ReadLine();
    student[i].studentName = input; 
    // I get an exception at that last line, after typing whatever string.I feel like I've done something horribly wrong.
}

Thanks :)

3
  • What's the exception that you get? Commented Jun 18, 2016 at 8:47
  • Welcome to Stack Overflow! Please go through the tour, the help center and the how to ask a good question sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. Commented Jun 18, 2016 at 8:48
  • An unhandled exception of type 'System.NullReferenceException' occurred in RandomCoding.exe Commented Jun 18, 2016 at 8:49

4 Answers 4

4

You declared an Array of Student but you didn't initialized it with Student instances. i.e student[i] is null.

After

for (int i = 0; i < N; i++) {

You should do

student[i] = new Student();
// Rest of the code.
Sign up to request clarification or add additional context in comments.

3 Comments

First line: Student[] student = new Student[N];
No, I mean inside the loop. new Student[N] only reserve the space, it doesn't allocate the space. Add the line i gave you as the first line in the loop, or right before you're using student[i], and it will fix the issue.
Worked! Basically what happened is that I allocated the N number of Student needed, but I didn't allocate the memory for each one of them right ? thanks again :)
0
using System;
using System.Collections.Generic;

public class Student
{
     public string studentName { get;set;}
     public long studentID;
     public int score1;
}

public class Test
{
    public static void Main()
    {
         var students = new List<Student>();
         for (int i = 0; i < 3; i++)
         {
             Console.WriteLine("Student {0}", i + 1);
             Console.Write("\t \t Name: ");
             string input = Console.ReadLine();
             students.Add(new Student {studentName = input });
         }                     
    }
}

Comments

0
Student[] student = new Student[N];
// the N is determined by a previous block    of code.
for (int i = 0; i < N; i++)
{
    student[i] = new Student();
    check = false; // ignore this one.
    Console.WriteLine("Student {0}", i + 1);
    Console.Write("\t \t Name: ");
    string input = Console.ReadLine();
    student[i].studentName = input; 
}

Comments

0
for (int i = 0; i < N; i++)
{
    check = false; // ignore this one.
    Console.WriteLine("Student {0}", i + 1);
    Console.Write("\t \t Name: ");
    string input = Console.ReadLine();
you are missing line below.
     student[i]= new student();
    student[i].studentName = input; 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.