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:
It means that the variable in question is pointed at nothing. I could generate this like so:
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:
- Always initialize your objects before you try to do anything with them.
- If you're not sure whether the object is null, check it with
object == null
.
What is NullReferenceException? And how to fix it?
Reviewed by Bloggeur DZ
on
08:37
Rating:
Helpful and very informative Programming
RĆ©pondreSupprimerarticle...
keep posting.