What is NullReferenceException? And how to fix it?

In this article we will see what is NullReferenceException.When you have a code and after executing it throws an error saying:

Object reference not set to an instance of an object.



 It means that the variable in question is pointed at nothing. I could generate this like so:
SqlConnection connection = null;
connection.Open();
That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.
To avoid this error:
  1. Always initialize your objects before you try to do anything with them.
  2. If you're not sure whether the object is null, check it with object == null.
What is NullReferenceException? And how to fix it? What is NullReferenceException? And how to fix it? Reviewed by Bloggeur DZ on 08:37 Rating: 5

1 commentaire:

Fourni par Blogger.