As a developer lot of time, you come across ” Object reference not set to an instance of an object ” error and sometimes along with the yellow screen of death. This error is very generic and does not give you a lot of information on what went wrong. In this blog, let’s explore different ways to analyse and fix this error.

The “Object reference not set to an instance of an object” error in C# typically occurs when you try to use a reference variable that has not been initialized.

The object or resource your code is looking for does not exists. Now it could be deleted, does not get initialized or an object or resource is moved to a different location.

c# Object reference not set to an instance of an object
c# Object reference not set to an instance of an object

Investigation & Fix: Object reference not set to an instance of an object

Here are some steps to help you resolve this error:

Check the line number where the error occurred: The error message usually includes the line number where the error occurred. Check that line and any lines that refer to the same object.

Make sure the object is initialized: If you are using a reference variable, make sure it is initialized before you use it. If it is not initialized, you can create a new instance of the object using the “new” keyword.

Check if the object is null: Before you use a reference variable, check if it is null. If it is null, you need to initialize it before you can use it.

Use debugging tools: If you are unable to determine the cause of the error, you can use debugging tools such as breakpoints, watch windows, and exception handling to help you identify the problem.

Application Logs

Every good web application record error or warning logs. If you explore these logs you will able to see which page or object is problematic.

There are lot of tools available to help you record these logs in your code. You do not need to choose any one of these. These tools are made to make your life easier.

  • log4net
  • elmah
  • more ….

Event Viewer

If you have access to the webserver or application server then your first option is to go to the event viewer and look at the application error logs. These logs will give you a hint on what page or class you have an error in. These logs also tell you about the problematic object.

Application Insight

For application hosted in Azure cloud, Application insight is the best tool to analyse and investigate Object reference errors.

Application insight is a very powerful tool in Azure that will give true insight performance of your application.

This tool has a feature “Failed requests”. This feature gives you list of all errors on your website. If you explore further more, you can also check exception against each error, user request map and telemetry of how application is interacting with different layers (for example database layer).

Example

Here is an example of how you can initialize an object:

MyObject myObj = new MyObject();

Here is an example of how you can check if a reference variable is null:

if (myObj != null)
{
    // use myObj
}
else
{
    // initialize myObj
}

By following these steps, you should be able to resolve the “Object reference not set to an instance of an object” error in C#.

Categorized in: