views:

206

answers:

4

A 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";

During compilation, it was fine, but i hit an error when I run my application. From my logs, the error occur at the above.

Question:

Is the error caused by the comment in line 3?

The error is "Object reference not set to an instance of an object"

I'm guessing the compiler treats the above code as 1 statement.

This code resides in the code behind of an aspx page. (aspx.cs)

//Backstory//

The aspx was tested in the test servers and was file. However after we deployed the page to the production server, there is an error at pageload(), the line where the error occur is at line 1 of my code example above.

It is just my suspicion that the error is caused by the comment.

Am I right?

+2  A: 

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

What exact error are you seeing?


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

This error has nothing to do with the code posted (unless you mis-represented the code when pasting it), and nothing to do with the // comment. You need to look elsewhere. Try looking at the stack-trace, or injecting debug/output messages. Or just step through the code to see where it actually blows up.

Marc Gravell
Sorry I amended my question.
Merv
+1  A: 

Does it work when you rewrite it to

a = b = c = "a String";

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

Aistina
+2  A: 

The compiler ignores everything to the right of the "//" in line 3 but lines 1, 2, and 4 are all still part of the same statement (as well as anything below line 4 until a ";" or block is reached).

Unless your example code lost something while posting it, this code should not even compile (parse error).


Ok, with your edited code the syntax is valid and should not be the cause of your error. Please post the error...

Tautologistics
A: 

There is no error in your code. It runs fine. If you are seeing an error, it is somewhere else.

recursive