3

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?

Winfield Trail
  • 5,535
  • 2
  • 27
  • 43
  • How are you calling this function, what is the source of ChatCurrent? – Mark Hall May 28 '12 at 00:01
  • It's passed by reference from the main thread, which in turn initializes it in a separate class. I figured out the problem, it's stupid, please see my answer below so you know why to mail flaming dog waste to my doorstep. – Winfield Trail May 28 '12 at 00:03
  • And what does the line before output? String concatenation does not throw on `null` values. – CodesInChaos May 28 '12 at 00:03

2 Answers2

9

String concatenation will treat null values as empty string

In string concatenation operations, the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.

http://msdn.microsoft.com/en-us/library/ms228504.aspx

But even if you would pass null directly to Console.Write, it won't throw an exception, it will just write nothing.

http://msdn.microsoft.com/en-us/library/zcwe8sfx%28v=vs.80%29.aspx

If value is a null reference (Nothing in Visual Basic), nothing is written and no exception is thrown.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • That's the ridiculous thing. `Console.Write` actually writes out text when I invoke it but comment out the line containing `ChatCurrent.Length`. I'm going insane. – Winfield Trail May 27 '12 at 23:55
  • @sudowned: of course it writes text: "`> `". Whereas `ChatCurrent.Length` throws an exception because you cannot access a property of a null reference. – Tim Schmelter May 27 '12 at 23:58
  • Yeah, it was writing the text **and the text I'd typed that was supposed to be in the buffer.** This is stupidity, this is entirely my own stupidity, but it's not what you think. See the answer I left. – Winfield Trail May 28 '12 at 00:01
1

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.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Winfield Trail
  • 5,535
  • 2
  • 27
  • 43