views:

647

answers:

10

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?

+5  A: 

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

Aaron Qian
In .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)
Mike
@Mike you cannot do such a thing;)
Maghis
Eh? I don't see why OOM conditions can't be handled gracefully.
JonB
I don't believe you can catch OutOfMemoryException anymore.
sixlettervariables
+7  A: 

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

mgroves
+4  A: 

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
+5  A: 

Stack overflow?

{◕ ◡ ◕}

Darth Continent
I would love to +1. but i wont
Midhat
@Mithat: That's okay, have a +1 from me anyway for your FAAABULOUS avatar!
Darth Continent
+1: cute <3 <3 <3
Juliet
+11  A: 
  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.

erikkallen
+7  A: 

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
+1  A: 

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
+1  A: 

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
A: 

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.

ajax81
+5  A: 

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
+1 for "Guessing is not an acceptable way to debug". It's amazing how often this happens.
Michael Burr