​​Solutions to fix the NullReferenceException (part 2)



To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them.



Solution 1: Check whether an object contains a null value or not using an if condition, as shown below:



In the above example, if(cities == null) checks whether the cities object is null or not. If it is null then display the appropriate message and return from the function.