Interview Question in C #

The technical manager asked me the following:
He created a class, declared an object, and initialized it. But in some cases, we may get a null reference exception.
He noted that there are 1000 possible reasons for such an exception and asked me to guess one reason.
I can’t figure it out. What is the reason (s) we can get such an exception?

+7
c # nullreferenceexception
source share
10 answers
  • You used a reference to an object explicitly set to null, or
  • You used a reference to an object that is implicitly set to null, or
  • Somewhere in your code or in the code you called, there is a throw new NullReferenceException() (which you shouldn't do, by the way). I don't know if this matters, as this is not a real null reference.

I cannot think of any of the other 997 reasons.

Edit: Thank you, Mark Byers, for point 3.

+11
source share

If this is a multi-threaded application, then another thread may arise and set the object to a null reference.

+8
source share

Stack overflow?

{β—• β—‘ β—•}

+7
source share

A few ways I can think of:

  • The constructor may NullReferenceException until it completes.
  • When accessing a property, the property may raise a NullReferenceException .
  • If you have try { } finally { } around the code, if it throws an exception, then it is finally executed, and the code may eventually NullReferenceException .
  • There may be an implicit conversion at the time of assignment, and the conversion code may be a NullReferenceException .

Here is a sample code for the latter:

 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. } } 
+7
source share

He created the class declared by the object and initialized it. But in some circumstances, we can get a "null link". He commented that there are 1,000 possible reasons for such an exception, and asked me to guess one reason. I can’t figure it out. What is (is) the reason (s), can we get such an exception?

The direct answer . I would tell the interviewer that you cannot debug code that you do not see. Ask to see an offensive line of code and a debugger.

A difficult answer . Assuming your interviewer is not an idiot, he probably feels that you have worked out your debugging skills. If you get an error message, you throw up your hands and give up right away, or you are trying to resolve it.

Guessing is not an acceptable way to debug an error. The first step is to reproduce the error on your computer.

Does it faithfully reproduce? If so, release the debugger.

If not, can you reproduce it intermittently or without determinism? Does the exception happen by accident in different places in the code or on different threads? If so, you probably have some kind of race condition or possibly a damaged pointer.

If not, ask who found the error to reproduce. When you follow the same steps as the person who initially found the error, can you reproduce? If yes, see above.

If not, is there a difference in the environments? Configuration files? Data in databases? Is the environment updated with the latest service packs, software updates, etc.

You will not be able to give your interviewer an answer, but you can give him a list of steps that you will take to get an answer.

+6
source share

Not an expert, but just a wild hunch, from memory?

+4
source share

You can always initialize something with 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(); } } } 
+4
source share

The object in question may contain other objects that are not initialized in the constructor of the main object. The question does not indicate where and when a null reference exception occurs.

999 to go.

+1
source share

In multi-threaded code, access to a variable can be obtained after creating the object, but before the variable has been assigned to its location.

+1
source share

I think the interviewer was really looking for how you solve the problem, that is, what troubleshooting steps will you take to solve the problem, which can be caused by thousands of different things.

0
source share

All Articles