1

Object reference not set to an instance of an object

I am getting this error sometimes. I have a page where I load questions and for some question system generate this error. Why do I get this error? Please help me.

4
  • Have you got any exception handling in place? Catch and rethrow the exception and examine the stack trace. Commented Jan 18, 2011 at 12:06
  • It means the object hasn't been given a value and therefore defaults to null. With the info you give that's the best answer you get. (paste relevant code!) Commented Jan 18, 2011 at 12:06
  • 1
    I disagree with the close vote. It's a real question, it just doesn't contain enough information to give a useful answer. Commented Jan 18, 2011 at 12:09
  • wht more you want to answer the question Commented Jan 18, 2011 at 12:26

3 Answers 3

2

It means that somewhere something dereferences a null reference. Crude example:

Foobar foo = null;
foo.DoSomething(); // this line will trigger a NullReferenceException

Here's what you can do:

  • compile in debug mode
  • run it until it crashes
  • examine the stack trace; it will tell you where the exception originates (unless you're doing stupid things like catching and rethrowing improperly). You may have to follow the chain of inner exceptions
  • if that doesn't provide sufficient information, step through your code with a debugger until right before the call that makes it crash. Examine any variables that might be null.

Once you have found the culprit, you need to find out why it is null and what to do about it. The solution can be one of:

  • fix incorrect program logic that causes something to be null that shouldn't be
  • check if something is null before dereferencing it
  • handle the exception at a point that makes sense, and translate it into something more meaningful (probably some sort of custom exception)
Sign up to request clarification or add additional context in comments.

Comments

1

You got null reference and if it can't handle it will show that message.

It can caused by some case

  1. The return value of a method is null
  2. A local variable or member field is declared but not initialized
  3. An object in a collection or array is null
  4. An object is not created because of a condition
  5. An object passed by reference to a method is set to null

I suggest you to track the null value using a debug mode

Comments

0

You get this error, whenever you are trying to make use of variable with Null value in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.