The NullReferenceException is an exception that will be thrown while accessing a null object.
The following example shows the code that throws the
NullReferenceException:In the above example, a
NullReferenceException will be thrown in the DisplayCities() function while accessing cities list using a foreach loop because the cities list is null. If the caller of the DisplayCities() function pass a null IList value then it will raise a NullReferenceException.