1

I am using Asp.net web api and the method calling another Class library which is working fine. For implementing Redis cache, I changed to class to interface. In this case the method is not fired which is in class library. I am getting the below error "Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException","StackTrace"

public class ArticleListingController : ApiController
    {
        public IArticleProvider _newsarticleProvider { get; set; }
        [HttpGet]
        [Route("Listing")]
        public IHttpActionResult GetArticlesListing(string sectionName, int RegionId, int Count)
        {
            List<Article> articleList = new List<Article>();
            if (!string.IsNullOrEmpty(RegionId.ToString()))
            {
                if (articleList != null)
                {
                    articleList = _newsarticleProvider.GetArticleListBySectionName(sectionName, RegionId, Count, ListingArticleCache);
                    return Ok(articleList);
                }
            }
            return NotFound();
        }
}

In Provider Interface Code

public interface IArticleProvider
{
        List<Article> GetArticleListBySectionName(string sectionName, int RegionId, int Count, int CacheTime);
 }

In Provider code

public class ArticleProvider 
    {

    public List<Article> GetArticleListBySectionName(string sectionName, int RegionId, int Count, int CacheTime)
    {return _articleRepositary.GetArticleListBySectionName(sectionName, RegionId, Count, CacheTime);              
     }
 }
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Balamurugan
  • 137
  • 1
  • 1
  • 13
  • 1
    Can you share the controller code which uses interface? Which line of code gives the exception? – Chetan Feb 28 '18 at 07:32
  • `[HttpGet] [Route("Listing")] public IHttpActionResult GetArticlesListing(string sectionName, int RegionId, int Count) { List
    articleList = new List
    (); if (!string.IsNullOrEmpty(RegionId.ToString())) { if (articleList != null) { articleList = _newsarticleProvider.GetArticleListBySectionName(sectionName, RegionId, Count, CacheTime); return Ok(articleList); } } return NotFound(); }`
    – Balamurugan Feb 28 '18 at 07:35
  • Please could you share it in your question where it has a chance of being legible? Also, that code doesn't provide any information about where `_newsarticleProvider` is instantiated. – ProgrammingLlama Feb 28 '18 at 07:39
  • When i come to below line `articleList = _newsarticleProvider.GetArticleListBySectionName(sectionName, RegionId, Count, ListingArticleCache);` getting null reference error – Balamurugan Feb 28 '18 at 07:40
  • So. Have you checked what's null using the debugger? Set a breakpoint, step through your code, hover over the variables. It will soon become clear where your problem lies. – ProgrammingLlama Feb 28 '18 at 07:42
  • `_newsarticleProvider` is getting null – Balamurugan Feb 28 '18 at 07:45
  • Are you setting _newsarticleProvider ? If not, of course it will be null. – ProgrammingLlama Feb 28 '18 at 08:01

2 Answers2

1

It looks like your _newsarticleProvider is not instantiated.

Check your configuration to make sure that your dependency injection is setup properly. If you're not using DI, make sure that you instantiate your provider in your controller's constructor.

Jeremie
  • 11
  • 2
1

First of all your class ArticleProvider does not implement your interface IArticleProvider. Then you need to create an instance of type ArticleProvider with reference type of IArticleProvider.

IArticleProvider _newsarticleProvider = new ArticleProvider();

Assuming you use some kind of DI container. If you do, you need to register your provider. Syntax can be different depending on the container you use. If you registered your provider withing DI resolver you won't need to create an instance of resolved type anymore and can use it throughout entire application. Just inject it using a class constructor:

    public class ArticleListingController : ApiController
    {
         private readonly IArticleProvider _newsarticleProvider;
         public ArticleListingController(IArticleProvider newsarticleProvider)
         {
             _newsarticleProvider = newsarticleProvider ?? throw new ArgumentNullException(nameof(newsarticleProvider));  
         }
     }
OlegI
  • 5,472
  • 4
  • 23
  • 31
  • I have using dependency injection and my problems get solved `public static class DIContainer { public static UnityContainer container; public static IUnityContainer Initialise() { container = new UnityContainer(); RegisterTypes(container); return container; } static void RegisterTypes(IUnityContainer container) { container.RegisterType(new InjectionConstructor(new ResolvedParameter())); } }` Thanks for your valuable answers!! – Balamurugan Mar 01 '18 at 14:46