1

I've hand-coded value in table dbo.Integrations where one of the column, IntegrationId="907BC4CF-4DC0-41FB-8EA4-87FE73A5BAE3". I am trying to compare Guid value in order to return a function.

But I get this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.' mcniDbC was null.

My code is:

/****************************************************************************************************************
 * File Name  : IntegrationsProcessor.cs
 * Description : This class is used for doing all Integration related database operation.
 * Created By : Anirudh Singh Rawat
 * Created Date : 6 April 2016
 ****************************************************************************************************************/

#region Namespace

using DBModel;
using MCNIDev.DBAccess;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web.Mvc;
using Utilities;

#endregion

namespace MCNIDev.Processor.Services
{
    public class IntegrationsProcessor
    {

        #region Private Member

        private MCNIDbContext mcniDbC;

        #endregion

        #region Public Methods

        /// <summary>
        /// This method is used for getting the existing 
        /// data from Integrations table.
        /// </summary>

        public Integration GetIntegrationRow()
        {
            
            return mcniDbC.Integration.FirstOrDefault(e => e.IntegrationId == Guid.Parse("907BC4CF-4DC0-41FB-8EA4-87FE73A5BAE3"));
        }

        #endregion
    }
}

This is the line of code causing the error:

return mcniDbC.Integration.FirstOrDefault(e => e.IntegrationId == Guid.Parse("907BC4CF-4DC0-41FB-8EA4-87FE73A5BAE3"));

Please not I am using this class to perform action on database by loosely coupling it with DbContext file MCNIDbContext in this case - and controller.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 4
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun May 27 '21 at 04:53
  • 1
    You declare `mcniDbC` variable but not yet assign value (DbContext) to it. – Yong Shun May 27 '21 at 04:55
  • I've already done that in another class and named it as "MCNIDbContext". – Anirudh Singh Rawat May 27 '21 at 05:05
  • 1
    @AnirudhRawat when you create an instance of ```IntegrationsProcessor``` and you want to call ```GetIntegrationRow``` you need also an instance of ```MCNIDbContext```. Otherwise you will get a null reference exception as you already pointed out. By default, since ```mcniDbC``` is a reference type and you don't initialize it anywhere during the creation of an instance of ```IntegrationsProcessor```, it's value would be null. That is needed is to supply an instance of ```MCNIDbContext``` to ```IntegrationsProcessor```. Define an constructor for ```IntegrationsProcessor``` that has one parameter – Christos May 27 '21 at 05:12
  • @AnirudhRawat ...this parameter would be of type ```MCNIDbContext```. Then set the supplied instance during constructor call to ```mcniDbC```. – Christos May 27 '21 at 05:15

1 Answers1

2

ISSUE

You are getting NullReferenceException as you haven't assigned value to mcniDbC.

private MCNIDbContext mcniDbC;

You declare it but you not assign, hence by default it is null.


SOLUTION: Constructor Injection

You can create a constructor to assign the mcniDbC.

namespace MCNIDev.Processor.Services
{
    public class IntegrationsProcessor
    {

        #region Private Member

        private MCNIDbContext mcniDbC;

        #endregion

        public IntegrationsProcessor(MCNIDbContext context)
        {
            mcniDbC = context;
        }

        ...
    }
}

Next, in your main program, you instantiate IntegrationsProcessor as below:

var integrationProcessor = new IntegrationsProcessor(new MCNIDbContext());

This method is known as Constructor Injection which is one of the Dependency Injection methods.

Yong Shun
  • 35,286
  • 4
  • 24
  • 46