C
C#•12mo ago
Just Some Bread

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");
}
}
}
32 Replies
Just Some Bread
Just Some BreadOP•12mo ago
Excuse the horrible readabilty im still learning 😅
exokem
exokem•12mo ago
Can you provide the full exception with stack trace and indicate which line of your provided code it points to?
Just Some Bread
Just Some BreadOP•12mo ago
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Program.Program.loadDataFromFile (System.String currFile) [0x002b6] in <13d2df00b486432580d913ddf7dcc117>:0
at Program.Program.Main (System.String[] args) [0x0000d] in <13d2df00b486432580d913ddf7dcc117>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Program.Program.loadDataFromFile (System.String currFile) [0x002b6] in <13d2df00b486432580d913ddf7dcc117>:0
at Program.Program.Main (System.String[] args) [0x0000d] in <13d2df00b486432580d913ddf7dcc117>:0
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at Program.Program.loadDataFromFile (System.String currFile) [0x002b6] in <13d2df00b486432580d913ddf7dcc117>:0
at Program.Program.Main (System.String[] args) [0x0000d] in <13d2df00b486432580d913ddf7dcc117>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Program.Program.loadDataFromFile (System.String currFile) [0x002b6] in <13d2df00b486432580d913ddf7dcc117>:0
at Program.Program.Main (System.String[] args) [0x0000d] in <13d2df00b486432580d913ddf7dcc117>:0
exokem
exokem•12mo ago
Also, if you add cs after the three you use to format code it will color it Has to be immediately after the and then newline
Just Some Bread
Just Some BreadOP•12mo ago
I didn't know that, thats cool thank you
exokem
exokem•12mo ago
Are you able to use a debugger to step through the code? This exception means that an operation like a method call is being done on a variable that is null, so it is possible something is not assigned correctly
Just Some Bread
Just Some BreadOP•12mo ago
I've tried to use the debugger but thats a whole issue within itself, I think its something with how I set it up. it doesnt seem to want to debug any cs file. It might have to do with the launch.json file but im not sure. Still new to these things haha.
exokem
exokem•12mo ago
What does the launch.json look like?
Just Some Bread
Just Some BreadOP•12mo ago
Currently, like nothing because I havent created one for this project since I cant seem to get the debugger working for any project. I went a bit crazy trying to get it to work
exokem
exokem•12mo ago
Have you tried opening the solution with visual studio? Vscode is not the best for beginners
Just Some Bread
Just Some BreadOP•12mo ago
I just tried to debug and out of nowhere its started to work Something about allowing developer permissions for my mac.
studentInfo[i][2] = Convert.ToString(total);
studentInfo[i][2] = Convert.ToString(total);
This is the line which says theres an issue Its on line 45
exokem
exokem•12mo ago
It's possible that studentInfo[i] is null In that case you would be trying to do null[2] = ... which would cause the error So you could make sure all subarrays in your studentinfo array are initialized
Just Some Bread
Just Some BreadOP•12mo ago
That sounds like that could be the issue the variable watch is saying that its currently null.
exokem
exokem•12mo ago
That would do it
Just Some Bread
Just Some BreadOP•12mo ago
When you initialise a string array does it set the items to null or empty strings?
exokem
exokem•12mo ago
null
Just Some Bread
Just Some BreadOP•12mo ago
Aha I found the issue I initialised a 2D array as string[][] not string[ , ] Seems to work now!
exokem
exokem•12mo ago
You could use both but [][] would require you to initialize every row to an array also
Just Some Bread
Just Some BreadOP•12mo ago
Yeah that explains why it would set it to null. Since i didnt initialise every row Except now its coming out as an IndexOutOfRangeException
exokem
exokem•12mo ago
How are you initializing the array now
Just Some Bread
Just Some BreadOP•12mo ago
string[,] studentInfo = new string[splitData.Length-1, 3];
string[,] studentInfo = new string[splitData.Length-1, 3];
exokem
exokem•12mo ago
You may not need the -1 in there Just a guess based on your loop
Just Some Bread
Just Some BreadOP•12mo ago
That might explain why its giving me an exception when i = 5;
exokem
exokem•12mo ago
What's the length of splitData
Just Some Bread
Just Some BreadOP•12mo ago
Should be 5.
exokem
exokem•12mo ago
Then you definitely don't need the -1 when you create the array
Just Some Bread
Just Some BreadOP•12mo ago
Still seem to be getting the error.
exokem
exokem•12mo ago
Is it the same line?
Just Some Bread
Just Some BreadOP•12mo ago
Yeah it seems to be running for a 6th time so i = 6, even though it should stop at 5 i changed it to i<studentInfo.GetLength(0)-1
exokem
exokem•12mo ago
The loop condition was ok
Just Some Bread
Just Some BreadOP•12mo ago
That fixed it I think its because studentInfo was a 2d array and .length usually works only for 1D arrays So it counted up more items
exokem
exokem•12mo ago
That's true
Want results from more Discord servers?
Add your server