Object reference set to an instance of an object.
Hi, just completing an assignment for uni that reads marks from a file and calculate the exam percentage and grade then outputs it to the console with the number of students who had that grade in the file. I came across a NullReferenceException and I'm not sure how to handle it.
using System;
using System.IO;
namespace Program{
class Program{
static void Main(string[] args){
string currFile = "5StudentsOneEachClassification.mark";
string[][] studentInfo = loadDataFromFile(currFile);
outputClassification(studentInfo);
}
static string[][] loadDataFromFile(string currFile){
string data = File.ReadAllText(System.IO.Directory.GetCurrentDirectory()+"/TestInputFiles/"+currFile);
string[] splitData = data.Split(new char[]{'\n'});
string[][] studentInfo = new string[splitData.Length-1][];
double capstoneTotal = 0, examTotal = 0, challTotal = 0, total = 0;
for(int i = 0; i<splitData.Length-1;i++){
string[] temp = splitData[i].Split(new char[]{':', '[', ']', ' ', ','});
for(int x = 0; x<temp.Length-1; x++){
if(temp[i] == "ID"){
studentInfo[i][0] = temp[i];
}
else if(temp[i] == "Challenges"){
challTotal = Convert.ToInt32(temp[i+2]) + Convert.ToInt32(temp[i+3]) + Convert.ToInt32(temp[i+4]) + Convert.ToInt32(temp[i+5]) + Convert.ToInt32(temp[i+6]) + Convert.ToInt32(temp[i+7]) + Convert.ToInt32(temp[i+8]) + Convert.ToInt32(temp[i+9]) + Convert.ToInt32(temp[i+10]) + Convert.ToInt32(temp[i+11]);
}
else if(temp[i] == "Exam"){
examTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "Capstone"){
capstoneTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "FirstName"){
studentInfo[i][0] = temp[i+1];
}
else if(temp[i] == "LastName"){
studentInfo[i][1] = temp[i+1];
}
}
challTotal = Math.Round((challTotal/35)*100, 2);
examTotal = Math.Round((examTotal/7)*100, 2);
capstoneTotal = Math.Round((capstoneTotal/100)*100, 2);
total = (challTotal*50/100) + (examTotal*25/100) + (capstoneTotal*25/100);
studentInfo[i][2] = Convert.ToString(total);
}
return studentInfo;
}
static void outputClassification(string[][] studentInfo){
int first = 0, two1 = 0, two2 = 0, third = 0, fail = 0;
for(int i = 0; i<studentInfo.Length-1; i++){
if(Convert.ToInt32(studentInfo[i][2]) >= 70){
first++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 60){
two1++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 50){
two2++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 40){
third++;
}
else{
fail++;
}
}
Console.WriteLine($"{first} - First");
Console.WriteLine($"{two1} - 2:1");
Console.WriteLine($"{first} - First");
Console.WriteLine($"{third} - Third");
Console.WriteLine($"{fail} - Fail");
}
}
}using System;
using System.IO;
namespace Program{
class Program{
static void Main(string[] args){
string currFile = "5StudentsOneEachClassification.mark";
string[][] studentInfo = loadDataFromFile(currFile);
outputClassification(studentInfo);
}
static string[][] loadDataFromFile(string currFile){
string data = File.ReadAllText(System.IO.Directory.GetCurrentDirectory()+"/TestInputFiles/"+currFile);
string[] splitData = data.Split(new char[]{'\n'});
string[][] studentInfo = new string[splitData.Length-1][];
double capstoneTotal = 0, examTotal = 0, challTotal = 0, total = 0;
for(int i = 0; i<splitData.Length-1;i++){
string[] temp = splitData[i].Split(new char[]{':', '[', ']', ' ', ','});
for(int x = 0; x<temp.Length-1; x++){
if(temp[i] == "ID"){
studentInfo[i][0] = temp[i];
}
else if(temp[i] == "Challenges"){
challTotal = Convert.ToInt32(temp[i+2]) + Convert.ToInt32(temp[i+3]) + Convert.ToInt32(temp[i+4]) + Convert.ToInt32(temp[i+5]) + Convert.ToInt32(temp[i+6]) + Convert.ToInt32(temp[i+7]) + Convert.ToInt32(temp[i+8]) + Convert.ToInt32(temp[i+9]) + Convert.ToInt32(temp[i+10]) + Convert.ToInt32(temp[i+11]);
}
else if(temp[i] == "Exam"){
examTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "Capstone"){
capstoneTotal = Convert.ToInt32(temp[i+1]);
}
else if(temp[i] == "FirstName"){
studentInfo[i][0] = temp[i+1];
}
else if(temp[i] == "LastName"){
studentInfo[i][1] = temp[i+1];
}
}
challTotal = Math.Round((challTotal/35)*100, 2);
examTotal = Math.Round((examTotal/7)*100, 2);
capstoneTotal = Math.Round((capstoneTotal/100)*100, 2);
total = (challTotal*50/100) + (examTotal*25/100) + (capstoneTotal*25/100);
studentInfo[i][2] = Convert.ToString(total);
}
return studentInfo;
}
static void outputClassification(string[][] studentInfo){
int first = 0, two1 = 0, two2 = 0, third = 0, fail = 0;
for(int i = 0; i<studentInfo.Length-1; i++){
if(Convert.ToInt32(studentInfo[i][2]) >= 70){
first++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 60){
two1++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 50){
two2++;
}
else if(Convert.ToInt32(studentInfo[i][2]) >= 40){
third++;
}
else{
fail++;
}
}
Console.WriteLine($"{first} - First");
Console.WriteLine($"{two1} - 2:1");
Console.WriteLine($"{first} - First");
Console.WriteLine($"{third} - Third");
Console.WriteLine($"{fail} - Fail");
}
}
}