[英]Object reference not set to an instance of an object. C# Interface
I know this question has been asked quite a few times, and I know the reason I am getting the error is because LicenseInfoRepository
is null, but I am not sure how to fix it. 我知道这个问题已经问过很多次了,我知道收到此错误的原因是因为LicenseInfoRepository
为null,但是我不确定如何解决它。
public abstract class VendorCodeGeneratorBase
{
protected ILicenseInfoRepository LicenseInfoRepository;
protected ICountryRegionRepository CountryRegionRepository;
protected IEnumerable<IBrandModel> BrandModels;
protected string GenerateVendorCodeUnEncoded(GenerateVendorCodeRequestModel requestModel)
{
int formatId = requestModel.VendorCodeFormat;
var strToConvert = "";
var brandBytes = new byte[5];
var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);
The error is happening on the line 错误正在发生
var brands = LicenseInfoRepository.GetSubscriptionBrandsForVendorCode(requestModel.KeyNumber);
I don't know how to instantiate the class that implements the interface. 我不知道如何实例化实现该接口的类。 This is the class 这是班
public class LicenseInfoRepository : ILicenseInfoRepository
{
private readonly IEstDbContext _estDbContext;
public LicenseInfoRepository(IEstDbContext estDbContext)
{
_estDbContext = estDbContext;
}
public List<VendorCodeSubscriptionBrandModel>
GetSubscriptionBrandsForVendorCode(int keyNumber)
{
return _estDbContext.SubscriptionBrands
.Include(sb => sb.Brand)
.Where(sb => sb.KeyNumber == keyNumber)
.ProjectTo<VendorCodeSubscriptionBrandModel>()
.ToList();
}
}
I have a folder named repositories and inside that is the LicenseInfoRepository.cs
class, and also inside that is a folder named Interfaces
that has the ILicenseInfoRepository.cs
interface. 我有一个名为repositories的文件夹,里面有一个LicenseInfoRepository.cs
类,里面还有一个名为Interfaces
的文件夹,它具有ILicenseInfoRepository.cs
接口。 That interface looks like this: 该接口如下所示:
namespace Data.Repositiories
{
public interface ILicenseInfoRepository
{
List<VendorCodeSubscriptionBrandModel>
GetSubscriptionBrandsForVendorCode(int keyNumber);
}
}
In the same folder where you have LicenseInfoRepository.cs
, you need an interface class ILicenseInfoRepository.cs
: 在拥有LicenseInfoRepository.cs
的同一文件夹中,需要一个接口类ILicenseInfoRepository.cs
:
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Mvc;
// Add/remove as appropriate
namespace YourProject.DAL // Change to your project / repository folder name
{
public interface ILicenseInfoRepository : IDisposable
{
List<VendorCodeSubscriptionBrandModel> GetSubscriptionBrandsForVendorCode(int keyNumber);
// Add any other methods that exist in LicenseInfoRepository.cs file
}
}
There may lines missing in you VendorCodeGeneratorBase
class, especially how you usually instantiate private ApplicationDbContext db = new ApplicationDbContext();
VendorCodeGeneratorBase
类中可能缺少行,尤其是通常如何实例化private ApplicationDbContext db = new ApplicationDbContext();
VendorCodeGeneratorBase
. 。 After you add the interface, please share errors / details so we can continue fixing your code. 添加界面后,请分享错误/详细信息,以便我们继续修复您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.