Hello,
I'm new to programming and I have some issues with a C# exercise I found online, and I can't understand the problem with my code. Every time I run it, it shows the problem: "Object reference not set to an instance of an object". I would truly appreciate it if someone can please help me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercises
{
class Program
{
static void Main(string[] args)
{
Console.Write("Type the number of students that took the tests: ");
int i = int.Parse(Console.ReadLine());
Student[] arrStudents = new Student[i];
for (int j = 0; j < i; j++ )
{
Console.Write("Type the name of the student: ");
arrStudents[j].name = Console.ReadLine(); //this is where I'm experiencing my problem, but I believe that all the code in this for loop is problematic
Console.Write("Type the student's first test score: ");
arrStudents[j].gradeA = int.Parse(Console.ReadLine());
Console.Write("Type the student's second test score: ");
arrStudents[j].gradeB = int.Parse(Console.ReadLine());
Console.Write("Type the student's third test score: ");
arrStudents[j].gradeC = int.Parse(Console.ReadLine());
}
Console.WriteLine("Here is the list of the students and whether they passed:");
foreach (Student student in arrStudents)
{
if (student.Above90() == true)
Console.WriteLine("{0} ----- PASSED", student.name);
else if (student.Above90() == false)
Console.WriteLine("{0} ----- DIDN'T PASS", student.name);
}
Console.ReadLine();
}
}
class Student
{
public int gradeA { get; set; }
public int gradeB { get; set; }
public int gradeC { get; set; }
public string name { get; set; }
public Student()
{
this.name = ".";
this.gradeA = 0;
this.gradeB = 0;
this.gradeC = 0;
}
public Student(string Name, int ScoreA, int ScoreB, int ScoreC)
{
this.name = Name;
this.gradeA = ScoreA;
this.gradeB = ScoreB;
this.gradeC = ScoreC;
}
public bool Above90 ()
{
if ((this.gradeA > 90) && (this.gradeB > 90) && (this.gradeC > 90))
return true;
else
return false;
}
}
}
I know it's not exactly the best coding, but I wanted to use everything I have learned so far.
Thank you in advance :-)
No comments:
Post a Comment