A Technical Lead asked me the following:
He created a class, declared an object and initialized it. But in some circumstance we may get "null reference" exception.
He commented that there are 1000 possible reasons for such exception and asked me to guess a single reason.
I am unable to figure it out. What is (are) the reason(s) ,we may get such an exception?

- 12,886
- 8
- 50
- 82

- 87
- 2
-
59I hope he explained the question better than you did... – David M Apr 01 '10 at 13:30
-
5What a dumb interview question. – spoulson Apr 01 '10 at 13:45
-
1It's not really dumb, it got him thinking and will have given the interviewer an idea of how he thinks and how he handles "dodgy" questions. I doubt the interviewer was interested in his actual answer but more as to how he arrived at it. – AndrewC Apr 01 '10 at 16:10
-
Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:44
-
Someone please close this as a duplicate of http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net ? I've already voted to close it, so can't close it again. – John Saunders May 15 '14 at 02:44
10 Answers
- You have used an object reference you have explicitly set to null, or
- You have used an object reference you have implicitly set to null, or
- Somewhere in your code, or in code called by you, there is the statement
throw new NullReferenceException()
(which you shouldn't do, by the way). I don't know if this counts, since it's not a real null reference.
I can't think of any of the other 997 reasons.
Edit: Thanks, Mark Byers, for point 3.

- 242,243
- 40
- 408
- 536

- 33,800
- 13
- 85
- 120
If it's a multi-threaded app, then some other thread could come along and set the object to a null reference.

- 25,181
- 9
- 71
- 121
Stack overflow?
{◕ ◡ ◕}

- 2,319
- 3
- 25
- 41
-
5
-
@Mithat: That's okay, have a +1 from me anyway for your FAAABULOUS avatar! – Darth Continent Apr 01 '10 at 13:38
A few ways I can think of:
- The constructor can throw a
NullReferenceException
before it completes. - When you access a property, the property can throw a
NullReferenceException
. - If you have a
try { } finally { }
around the code, if it throws an exception the finally runs and the code in the finally could throw aNullReferenceException
. - There could be an implicit conversion during the assignment and the code for the conversion throws a
NullReferenceException
.
Here's example code for the last:
class Foo {}
class Bar
{
public static implicit operator Foo(Bar bar)
{
throw new NullReferenceException();
}
}
class Program
{
public static void Main()
{
Foo foo = new Bar(); // This causes a NullReferenceException to be thrown.
}
}

- 811,555
- 193
- 1,581
- 1,452
He created a class, declared an object and initialized it. But in some circumstance we may get "null reference" exception. He commented that there are 1000 possible reasons for such exception and asked me to guess a single reason. I am unable to figure it out. What is (are) the reason(s) ,we may get such an exception?
Straightforward answer: I'd tell the interviewer that you can't debug code you can't see. Ask to see offending line of code and a debugger.
Not-so-straightforward answer: assuming your interviewer isn't an idiot, he probably feeling you out for your debugging skills. If you get a crappy bug report, do you throw your arms up and surrender right away, or do you attempt to resolve it.
Guessing is not an acceptable way to debug the error. The first step would be reproducing the bug on your machine.
Does it reproduce reliably? If yes, get your debugger out.
If no, can you reproduce it intermittently, or non-deterministically? Does the exception occur randomly in different places in code or on different threads? If yes, you probably have some sort of race condition, or maybe a corrupted pointer.
If no, ask whoever found the bug to reproduce. When you follow the same steps as the person who originally found the bug, can you reproduce? If yes, see above.
If no, is there are a difference in environments? Configuration files? Data in the databases? Is the environment updated with the latest service packs, software updates, etc?
You won't be able to give your interviewer an answer, but you can give him a list of steps you'd take to eventually get to an answer.

- 80,494
- 45
- 196
- 228
-
2+1 for "Guessing is not an acceptable way to debug". It's amazing how often this happens. – Michael Burr Apr 03 '10 at 06:49
Not an expert, but just a wild guess, out of memory?

- 4,477
- 2
- 24
- 27
-
6In .NET I believe there is an OutOfMemoryException, which is what you'd get (unless you handled the OutOfMemoryException and continued like nothing was wrong :D) – Karmic Coder Apr 01 '10 at 13:34
-
2
-
-
You can always initialize something to a null value;
public class MyClass
{
// initialized to null
private string _myString = null;
// _myString is initialized, but this throws null reference
public int StringLength { get { return _myString.Length(); } }
}

- 242,243
- 40
- 408
- 536
The object in question may contain other objects that are not initialized in the main object's constructor. The question doesn't specify where or when the null reference exception is occurring.
999 to go.

- 74,184
- 40
- 190
- 334
In Multi-threaded code the variable can be accessed after the object has been created, but before the variable has been assigned to its location.

- 31,137
- 42
- 147
- 238
I think the interviewer was actually looking for how you'd go about solving the problem, ie what troubleshooting steps you'd take to solve a problem that could be caused by a thousand different things.

- 7,181
- 6
- 48
- 65