A NullReferenceException
is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.
For example:
When a string method of an empty string accessed:
string str = string.Empty;
str.ToLower(); // throw null reference exception
When a property of a null object accessed:
Public Class Person {
public string Name { get; set; }
}
Person objPerson;
objPerson.Name /// throw Null refernce Exception