-1

I've looked everywhere and I cannot seem to find the root of the problem, nor how to fix this problem. My code below is sorting out an array using a bubble sort and labeling names a-z. That works fine, but this is not the problem. The problem is that when the code runs along the line I have indicated, the error shows. And I don't even know where to start about getting this problem fixed. Been at it for 2+ hours.

        string[] temp = new string[3];
        for (int passes = 0; passes < classMateInfo.Length; passes++)
        {
            for (int pos = 0; pos < classMateInfo.Length - 1 - passes; pos++)
            {
                //The problem is this line below. The error shows up for this.
                if ((classMateInfo[pos].first).CompareTo((classMateInfo[pos + 1].first)) == 1)
                {
                    temp[0] = classMateInfo[pos].first;
                    temp[1] = classMateInfo[pos].last;
                    temp[2] = classMateInfo[pos].ID;
                    classMateInfo[pos] = classMateInfo[pos + 1];
                    classMateInfo[pos + 1].first = temp[0];
                    classMateInfo[pos + 1].last = temp[1];
                    classMateInfo[pos + 1].ID = temp[2];
                }
            }
        }

Do not bother linking me about other people's problems that are similar to mine. I have a hard time understanding other people's answers and my code is different so if someone could point me into the right direction and give me more detail into why this exception is being thrown, that'd be great.

My question is, to clarify, why am I getting a null exception thrown at me? Where to look to fix the problem?

Thanks.

bsd u
  • 77
  • 1
  • 6
  • 2
    you need to put a breakpoint at the if statement and check values of pos during each iteration. Do debugging first – tariq Jun 12 '15 at 04:59
  • Show us the code for classMateInfo, what is it exactly, how did you instantiate it, with what value? – Hamid Mosalla Jun 12 '15 at 05:03
  • break; doesn't change anything. I do not know how this null exception came to be. I doubt debugging would help me if I don't even know what the root of the problem is. – bsd u Jun 12 '15 at 05:04
  • 1
    Look for null values in anything before a dot. Whenever you do `null.something` you'll get the error. In the line where you're having the error, `classMateInfo[pos]` could be the problem because it's followed by `.first`, `classMateInfo[post].first)` could be the problem because it's followed by `.CompareTo`, and `classMateInfo[pos + 1]` could be the problem because it's followed by `.first`. – Ed Gibbs Jun 12 '15 at 05:05
  • My struct containing the first and last names is called with classMatesInfo – bsd u Jun 12 '15 at 05:05
  • When you break in the debugger, open the Immediate window, type in the expressions from my comment (`classMateInfo[pos]`, etc.), and hit enter. If any of them come up as null (it may also say undefined - I'm blanking on this) then they're the problem. If you still don't figure it out, post the contents of the `classMateInfo` array. – Ed Gibbs Jun 12 '15 at 05:07
  • Your 2 hours would have been better spent learning how to use your debugger. When the exception is thrown the debugger should be sitting on the offending line of code. Hover over the variables on the line or type them in the watch window - something will be null. Also look for clues in the values of `passes` and `pos`. – jaket Jun 12 '15 at 05:19
  • What is this 'immediate window' you speak of? – bsd u Jun 12 '15 at 05:19
  • I found the null's, thank you very much. It is my first and last name I am trying to add to the struct of type array. Now, to figure out how to fix this... – bsd u Jun 12 '15 at 05:21
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ondrej Tucny Jun 13 '15 at 12:14

1 Answers1

1

If you get a NullReferenceException at this line:

if ((classMateInfo[pos].first).CompareTo((classMateInfo[pos + 1].first)) == 1)

This means that the classMateInfo array either contains a null value, or one of the elements in the array has a null value for first.

Can't help you any further, I don't know the contents of the array.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72