Object reference not set to an instance of an object
I am getting this error sometimes. I have a page开发者_如何学Go where I load questions and for some question system generate this error. Why do I get this error? Please help me.
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)
You got null reference and if it can't handle it will show that message.
It can caused by some case
- The return value of a method is null
- A local variable or member field is declared but not initialized
- An object in a collection or array is null
- An object is not created because of a condition
- An object passed by reference to a method is set to null
I suggest you to track the null value using a debug mode
You get this error, whenever you are trying to make use of variable with Null value in it.
精彩评论