开发者

ASP.NET Error Object reference not set to an instance of an object

开发者 https://www.devze.com 2023-02-05 11:40 出处:网络
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

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

  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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消