C
C#•7mo 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 Bread•7mo ago
Excuse the horrible readabilty im still learning 😅
exokem
exokem•7mo 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 Bread•7mo 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•7mo 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 Bread•7mo ago
I didn't know that, thats cool thank you
exokem
exokem•7mo 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 Bread•7mo 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•7mo ago
What does the launch.json look like?
Just Some Bread
Just Some Bread•7mo 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•7mo ago
Have you tried opening the solution with visual studio? Vscode is not the best for beginners
Just Some Bread
Just Some Bread•7mo 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•7mo 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 Bread•7mo ago
That sounds like that could be the issue the variable watch is saying that its currently null.
exokem
exokem•7mo ago
That would do it
Just Some Bread
Just Some Bread•7mo ago
When you initialise a string array does it set the items to null or empty strings?
exokem
exokem•7mo ago
null
Just Some Bread
Just Some Bread•7mo ago
Aha I found the issue I initialised a 2D array as string[][] not string[ , ] Seems to work now!
exokem
exokem•7mo ago
You could use both but [][] would require you to initialize every row to an array also
Just Some Bread
Just Some Bread•7mo 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•7mo ago
How are you initializing the array now
Just Some Bread
Just Some Bread•7mo ago
string[,] studentInfo = new string[splitData.Length-1, 3];
string[,] studentInfo = new string[splitData.Length-1, 3];
exokem
exokem•7mo ago
You may not need the -1 in there Just a guess based on your loop
Just Some Bread
Just Some Bread•7mo ago
That might explain why its giving me an exception when i = 5;
exokem
exokem•7mo ago
What's the length of splitData
Just Some Bread
Just Some Bread•7mo ago
Should be 5.
exokem
exokem•7mo ago
Then you definitely don't need the -1 when you create the array
Just Some Bread
Just Some Bread•7mo ago
Still seem to be getting the error.
exokem
exokem•7mo ago
Is it the same line?
Just Some Bread
Just Some Bread•7mo 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•7mo ago
The loop condition was ok
Just Some Bread
Just Some Bread•7mo 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•7mo ago
That's true