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
22 Replies
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 nullwhat do i have to do to fix that? all my methods don't run in the console
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 exceptioni am lost
because i have never used ref, is there another way?
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 =
operationwhat line of code can be used to fill in the names box?
if you do
that makes a box named
foo
that can only hold int
s and then we also put a value of 1
in that boxi assume all my methods have the same problem
very possible
can you paste the code you currently have where you declare
names
?paste the code in that chat?
yes using the ``` backtick markdown to put it in a code block
```
like this
```
not all your code just the part where you declare
names
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]); }
}
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]); }
}
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
IE
is declaring foo
with a value of 1
so because i'm talking about name grade age
would it be int intgrade= 0;
int intage=0;
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 multiplei don't get it, can you show me?
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)so i declare int[] grade = 0;
something like that?
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
i belive i got the method to be working
thank you!
i added this which made the console display instead of crashing/freezing