I’ve got the following code:
public void DrawInput(string ChatCurrent){
int uCursorTop;
int uCursorLeft;
uCursorLeft = Console.CursorLeft;
uCursorTop = Console.CursorTop;
Console.SetCursorPosition(0, uCursorTop);
Console.Write("> "+ChatCurrent+" ");
Console.SetCursorPosition(ChatCurrent.Length, uCursorTop);
}
Except for the final line, it behaves properly. The final line throws System.NullReferenceException: Object reference not set to an instance of an object. The weird thing? Specifically, accessing ChatCurrent.Length is what’s making it fail. The line immediately before, which echoes the string’s contents, works just fine.
What’s going on?
Here’s what was happening:
I got the error as described above. When I commented out the Length measurement, I typed stuff in to see if the text would cause the error too. When it didn’t, I put it back and tried other experiments.
I was consistently not typing anything while I tested, which in turn meant that the variable was declared but not initialized. I have been trying to fix this problem for two hours.
To wit, in case someone else has this problem: Make sure that your variable is initialized. It’s as important as declaring it in this context.