C
C#7mo ago
Nate

C# Array and Bubble Sort

hello i'm learning arrays and bubble sort. My program does not display anything in the console after selecting something from the menu. could someone help me? i'm a begineer to coding so any help with be greatly appreiecated
No description
22 Replies
PixxelKick
PixxelKick7mo ago
You're program is throwing a null reference exception there, thats causing execution to halt based on the code on that line, its because the variable names is null
Nate
NateOP7mo ago
what do i have to do to fix that? all my methods don't run in the console
PixxelKick
PixxelKick7mo ago
so you're trying to evaluate what null.Length is which isnt possible, so it throws the null ref exception the variable names should have its value set before you hit that code effectively you're trying to access a variable before you gave it a value, so that throws an exception
Nate
NateOP7mo ago
i am lost because i have never used ref, is there another way?
PixxelKick
PixxelKick7mo ago
variables are like a box you can store values in you have a box you have written the label names on with a marker, but the box is still empty your method ShowUnsortedList() is now trying to get the stuff inside the names box... but the box is empty so it goes "wat?" and the program crashes in C# we put stuff in our boxes (the variables) with the = operation
Nate
NateOP7mo ago
what line of code can be used to fill in the names box?
PixxelKick
PixxelKick7mo ago
if you do
int foo = 1
int foo = 1
that makes a box named foo that can only hold ints and then we also put a value of 1 in that box
Nate
NateOP7mo ago
i assume all my methods have the same problem
PixxelKick
PixxelKick7mo ago
very possible can you paste the code you currently have where you declare names?
Nate
NateOP7mo ago
paste the code in that chat?
PixxelKick
PixxelKick7mo ago
yes using the ``` backtick markdown to put it in a code block
like this
like this
``` like this ``` not all your code just the part where you declare names
Nate
NateOP7mo ago
static void ShowUnsortedList() {
Console.WriteLine("Name, Grade, Age List from File (Unsorted):"); for (int i = 0; i < names.Length; i++) { Console.WriteLine(names[i] + ", " + grades[i] + ", " + ages[i]); }
}
PixxelKick
PixxelKick7mo ago
nah thats evaluating names somewhere else you have declared names earlier declaring is when you first "make" the variable exist usually in the format of
type name = value;
type name = value;
IE
int foo = 1;
int foo = 1;
is declaring foo with a value of 1
Nate
NateOP7mo ago
so because i'm talking about name grade age would it be int intgrade= 0; int intage=0;
PixxelKick
PixxelKick7mo ago
you're accessing what appears to be an array or list of names, grades, and ages so its likely more than just an int if its an array it'll have [ and ] to indicate its multiple
Nate
NateOP7mo ago
i don't get it, can you show me?
PixxelKick
PixxelKick7mo ago
int foo = 1; means my box labeled foo can only hold a single int, it is an int, thats it int[] foos = [ 1, 2, 3, 4, 5] is an array of ints, which is sorta like a list of things (but List is its own thing in C# so we dont mix em up)
Nate
NateOP7mo ago
so i declare int[] grade = 0; something like that?
PixxelKick
PixxelKick7mo ago
arrays need to be wrapped in [ ] int[] grades = [0]; would work, and specifically would declare "An array of grades 1 item long, with that item #0 having a value of 0" (in programming we are 0 based usually so stuff like arrays and lists start at 0, not 1, so the first item is "#0" if you will) and then you access it with [] as well. myArray[4] would get the item at index 4 inside of myArray
Nate
NateOP7mo ago
i belive i got the method to be working thank you!
Nate
NateOP7mo ago
No description
Nate
NateOP7mo ago
i added this which made the console display instead of crashing/freezing
Want results from more Discord servers?
Add your server