How to avoid NullReferenceException errors?

15 November 2021 at 10:00 by ParTech Media - Post a comment

With the advent of Object-Oriented Programming, programmers got the opportunity to:

  • Define a human-understandable structure
  • Develop the logic of the code at the user’s convenience
  • Making it suitable for better performance, and maintainability

Features like encapsulation, inheritance, polymorphism, and abstraction offer ways for building a robust, loosely coupled, and efficient code. But, the foundation of OOPs lies with the classes and the objects. Classes are templates or blueprints for which objects are created and it contains a set of properties and methods which can be accessed by objects.

From C# point of view, the variables fall under either of the two categories - value type, and reference type. Most of the primitive data types are value types, where the data is directly stored in the memory. However, in the case of reference types, the address of the location where the value is stored is normally held.

Both primitive and custom classes are used in the application for achieving business requirements. Until the code works in the happy path, no one has any issues as it's business normal. The moment it starts going wrong, it becomes a pain for developers and support teams to figure out the reason for the error.

When proper logging, error handling, and standards are not followed, then it becomes even more difficult to address the issue.

And whenever we talk about exceptions and errors, it is hard to ignore NullReferenceExceptions as it is one of the most common and annoying errors out there. They can occur at any part of the code and can abruptly end the process in the middle.

In this post, we will understand the NullReferenceException in detail and how to handle and avoid this exception.

Tables of contents

  1. Introduction to NullReferenceException
  2. Common Scenarios of NullReferenceException
  3. Conclusion

Introduction to NullReferenceException

NullReferenceException is the most common exception that can occur when any properties or methods point to null instead of holding any value. These have a sizable weightage when it comes to bugs that happen across applications. Luckily, this exception can be handled with simple condition checks before we try to access the particular property/method. Let's see about them in the next section.

Common Scenarios of NullReferenceException

Scenario - Getting the exception while working with strings. Below is a sample code illustrating the issue.

public static void NullReferenceException()
​    {
​      string sample = null;
​      Console.WriteLine(sample); 
​      sample.ToUpper();
​    }

Here, in the above example, there is a variable of type string and it has been deliberately assigned as null for our training purpose. In the next line, the sample value has been written in the console window. At that instance, no error would occur.

However, on executing the next line, NullReferenceExcception would occur, stating that the variable sample was null. Reason - ToUpper is an in-built method for string, which will convert the string into a complete upper case. Since the value is null, the ToUpper method fails with the above exception.

To handle this, a check before we perform any activity with the variable would be sufficient. There are multiple ways to achieve it; both the conventional way of having an if condition for null check or using the nullable annotation after the variable will work.

Conventional way - if(sample != null)

New technique - sample?.ToUpper()

Also, when creating a definition of a method, add nullable types of variables as input parameters, say string? Sample.

By adding this, the compiler would indicate during the development time that there is a chance that the input parameter could be null - Consider having a check for it.

Similarly, the same issue can happen with arrays, lists, custom class objects, and so on. And it is advisable to follow the above-mentioned concept to avoid the exception.

Let's take another example, where we have a class Animal and it has few properties to it, like Name, Type, and so on. Let's see the possible scenarios of NullException here.

 public static void NullReferenceException(Animal data)
​    {
​      var animalName = data.Name;
​      Console.WriteLine(animalName);
​    }

In the above example, the Animal object is being passed as an input parameter and some properties of the object are being accessed here.

In this case, it is first advised to check if the animal object holds any data and then move on to see if there is any value for the Name property. As mentioned earlier, it can be done both ways for the animal object.

And for name, below are some ways how it can be dealt with.

Using Null Coalescing

Example, var animalName = data?.Name ?? alternateData

The null coalescing operator checks if the value preceding it has null or not. If it's null, then it assigns the value that succeeds it, else it assigns back the preceding value.

Using Ternary Operator

Example var animalName = data?.Name != null ? data?.Name : alternateData

Here, the condition of checking against null has to be set by the developers; also it can be used to validate it against any other values as well.

Let's see some common NullReferenceExceptions for Arrays, List, Dictionary,

Array

Int[] number = null; 
Number[index];

List

List<int> number = null;
number[index];

Dictionary

Dictionary<int, int> number = null;
number[index];

Accessing the index of a null object will result in NullException. Here it is explicitly set, but when running the program, the value could be set at runtime. As mentioned earlier, any of the null handling techniques can be used according to the suitability of the data type.

Key points to avoid NullException

  • Always check if there is data in the object before accessing any of its properties.
  • Before applying any manipulation/data obtaining logic to any property, check if they are not null.
  • In case of being null, either throw the error using custom exceptions or use try-catch block and log them.

Conclusion

NullReferenceExceptions are the most common and tiresome exception that can occur in any application. The developers and the support team have to run different scenarios and pseudo runs in the code to figure out the reason for it. Try using the methods highlighted in this post and solve NullReferenceException errors in the future.

Latest