Error: An object reference is required for the non-static field

Webner Solutions
2 min readFeb 22, 2021

--

Object Reference

In one of my .net reference projects, I had a need to create a new model and its corresponding interface, repository, and client class. In a method of the existing controller class, I need to create a record of that model class but I don’t want to use this new class’s interface as dependency injection for. So to call the method from the repository class, I tried to directly access the method using the name of the Interface or the repository class like:

ITestRepository.createAsync();
Or
TestRepositoryClient.createAsync();

Here, ITestRepository is the interface and TestRepositoryClient class is implementing the interface ITestRepository.

But I was getting the following error in this way:
An object reference is required for the non-static field, method, or property

Error: An object reference is required for the non-static field
Error: An object reference is required for the non-static field

Then I tried to create an object from the interface class like below:

ITestRepository myTestRepo = new ITestRepository();

But it also gave me the following error:
Cannot create an instance of the abstract class or interface

Error: An object reference is required for the non-static field
Error: An object reference is required for the non-static field

So to resolve this issue, I created an instance of the repository like below:

ITestRepository myTestRepo = new TestRepositoryClient();

And called method on this object like below:

myTestRepo.createAsync();

So this is the way to create an instance of a repository class using an interface.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Webner Solutions Code To Sucess
Webner Code To Sucess

Originally published at https://blog.webnersolutions.com on February 22, 2021.

--

--

Webner Solutions
Webner Solutions

Written by Webner Solutions

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

No responses yet