Inner Exception: Object reference not set to an instance of an object.

Error Rendering Controller: <Controller-Name>. Action: <Controller-method>: An unhandled exception occurred.

Inner Exception: Object reference not set to an instance of an object.

Many times, we encounter this type of error, so let's understand what the error actually wants to convey.

If we look at the "Inner Exception: Object reference not set to an instance of an object," the first line points out that the error happens at SCPlayground.Controllers.CardController.GetCardModel(Item dataSource) in C:\SCPlayground\SCPlayground\Controllers\CardController.cs:line 35.

This error can stem from two primary causes:

  1. A discrepancy in fields between the controller and the data template in Sitecore.

  2. Failing to specify the data source path in the presentation details.

Checking datasource path

Click on the page, navigate to presentation details, and select <Controller-name>.

If you observe a blank field for the datasource in the presentation details, it signifies an error. You must specify the data path to see thing.

Click on "Browse" and select the appropriate content for it.

Checking .Net MVC Controller

Let's understand with an example. We'll use the card as an example, so first, we examine the card controller.

Make sure the field name used in the controller matches the template field.

In CardController.cs:

model.title = eventItem.Fields["CardTitle"].Value;

In the Sitecore Template field:

Verify all fields match the above.

Suppose the field name is "CardTitle," and you specify the field name as "Card Title."

string title = eventItem.Fields["Card Title"].Value;

It does not meet the requirements, so also check for spaces.

If there are no errors, then the issue lies in the datasource location. Visit the Template blog for guidance on creating templates.

For more information, visit the Sitecore's website for further details.