7

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?

Graham Clark
  • 12,614
  • 8
  • 46
  • 78
amutha
  • 87
  • 2
  • 59
    I hope he explained the question better than you did... – David M Apr 01 '10 at 13:30
  • 5
    What a dumb interview question. – spoulson Apr 01 '10 at 13:45
  • 1
    It'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 Answers10

11
  1. You have used an object reference you have explicitly set to null, or
  2. You have used an object reference you have implicitly set to null, or
  3. 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.

Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
erikkallen
  • 31,744
  • 12
  • 81
  • 116
8

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

Matthew Groves
  • 22,897
  • 8
  • 64
  • 109
7

Stack overflow?

{◕ ◡ ◕}

Darth Continent
  • 2,251
  • 2
  • 25
  • 41
7

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 a NullReferenceException.
  • 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.
    }
}
Mark Byers
  • 719,658
  • 164
  • 1,497
  • 1,412
6

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.

Juliet
  • 76,873
  • 44
  • 191
  • 224
4

Not an expert, but just a wild guess, out of memory?

Aaron Qian
  • 4,047
  • 2
  • 22
  • 26
4

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(); } }
}
Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
1

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.

MusiGenesis
  • 71,592
  • 38
  • 183
  • 324
1

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.

C. Ross
  • 28,735
  • 39
  • 139
  • 230
0

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.

Daniel Szabo
  • 6,926
  • 5
  • 44
  • 61