C # compilation question: newlines in 1 expression throwing an error

Simple question

Consider this piece of code in C #

String a;
String b;
String c;

 1. a =  
 2. b =
 3. //Comment
 4. c = "a String";

      

At compile time it was fine, but I got into an error when starting the application. From my logs, the error occurs with the above.

Question:

Is the error caused by the comment on line 3?

Error: "Object reference not set to object instance"

I assume the compiler treats the above code as an instruction.

This code is in the code behind the aspx page. (Aspx.cs)

// Background //

Aspx was tested on test servers and was a. However, after we deployed the page to the production server, there was an error in pageload (), the line where the error occurred is on line 1 of my example code above.

It's just my suspicion that the comment was causing the error.

I'm right?

0


source to share


4 answers


The original code was not legal. Your edited code is fine; all 3 variables are assigned the same string ("string").

What exact error are you seeing?




(question updated to include "Object reference not set to object instance")

This error has nothing to do with the code posted (unless you provided the code on embed) and has nothing to do with the comment //

. You need to look elsewhere. Try looking at the stack trace or typing debug / output messages. Or just step through the code to see where it really explodes.

+2


source


The compiler ignores everything to the right of the "//" on line 3, but lines 1, 2, and 4 are still part of the same statement (as does everything below line 4 before the ";" or block).

Unless your code of code has lost something while posting it, that code shouldn't even compile (parse error).




Ok, with your edited code, the syntax is valid and shouldn't be the cause of your error. Submit a bug ...

+2


source


Does it work when you rewrite it to

a = b = c = "a String";

      

? If so, then it's clear what is causing your problem;)

+1


source


There is no error in the code. This is normal. If you see an error, it is somewhere else.

0


source







All Articles