Hello /wsr/
I had a thread recently but I've not resolved the problem entirely.
I keep getting this error:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at FileDemo.SortArray2(String[] array)
at FileDemoTest.Main(String[] args)
I'm not sure why I'm getting this because I initialise it here:
FileDemo obj = new FileDemo();
string[] array = new string[600];
This is the file that displays it:
else if (option == 3)
{
Console.WriteLine("Option 3 chosen........");
obj.ReadFromFile3( array);
Console.Write("Not sorted: \n");
obj.DisplayArray2(array);
Console.Write("\nSorted: \n");
//obj.GetCategory(array);
obj.SortArray2( array);
obj.DisplayArray2(array);
}
But the problem lies here:
public void SortArray2(string[] array)
{
int i, j, l;
l = array.Length;
string temp;
for (i = 0; i < l; i++)
{
for (j = 0; j < l - 1; j++)
{
HERE if (array[j].CompareTo(array[j + 1]) > 0)
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
HERE is the line that brings up the error.
Please help me /wsr/.
>>356701
Array.Length gives you the number of slots in the array, not the number of String objects.
Any slots you've not put a String in will be null, and null doesn't have a CompareTo method.
You need to find the last String rather than just asking the array how long it is.
>>356721
Okay so I know I have a string array of 400 words.
How do I use the last string to compare it with?
I've tried:
l = Convert.ToInt32(array[20]);
But this achieves nothing.
>educational institution offering classes in MS programming languages
ISHYGDDT
>>356730
The kind of questions you keep asking, it's clear you don't understand procedural programming, OO programming, or pointers.
Go get Head-First Java*, start at the beginning, and work through it. Actually do the exercises, even the word-searches. Once you do, you'll be able to come back to the stuffed you're doomed now, and you'll actually understand what's going on.
You will spend more time staggering in the dark doing what you're doing than you will going away, finishing Head-First Java, and coming back. You don't learn anything by failing, the time you learn is the bit where you do it right and it works. All the time you're spending now trying random, wrong things is just wasted time: you're not learning anything.
> >java
Some people will tell you that Java is a bad language to learn. They are stupid people who don't understand programming and find it hard to learn languages. Don't be them. Once you understand how to program, you'll find a lot of the stuff you learned in Java transfers to C#, and a lot of the stuff you'll learn in C# transfers to C++ and to JS. Learning other languages in the same paradigm is *easy*: it's just a different library and a slightly different way of writing what you want it to do. Anyone that tells you otherwise is thinking of how hard it was for them to learn one language, because they didn't (and still don't) understand what's going on.
* https://libgen.pw/view.php?id=474519
>>356823
What if I possibly have a project due in a few days?
>>356824
Still do it.
In the past week, you've struggled with:
- what an object is
- what a pointer is
- how parameters are passed
- how to invoke a function
- what null is
- how an array works
- how to cast between data types
These are all basic things that (assuming you're doing a class) will have been covered in class. If you don't understand this basic stuff, then you're not programming, you're just guessing.
>>356826
Honestly, the other projects this year (year 1 CS degree) have been fine, could do them in a week and get a good score.
This unit is harder than the others. I didn't keep up, missed the first deadline and now the final deadline is coming up. It's a slippery slope but I don't want to repeat the year. I know I've screwed up though.
Monday is the deadline so you can imagine I'm a little stressed.
I don't actually enjoy coding I think.
Thanks for the help though.