2 Replies - 933 Views - Last Post: 13 June 2016 - 02:45 PM Rate Topic: -----

#1 emersonc12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-May 16

Object Reference Not set to an instance of an object and/or Value Cann

Posted 03 May 2016 - 08:34 AM

So for the last 2 weeks I've been trying to figure out why since I included in my repository the diaries field that my unit tests that have no affiliation to the DiariesIndicator field are showing up failed. The only relation that I can find between the tests are that they are all part of the same repository when it comes to mapping and including the data fields. I'm showing the same error for each failed test "Object Reference not set to an instance of an object." Before I was continuously getting Value Cannot be null. but now i'm getting this new error. I am very new to the C# world and working with backend code. I am very familiar with front end UI/HTML/CSS code so if anyone can help explain why this is occurring and how I can fix it I would greatly appreciate it. here is the code I have in the repository:

public IQueryable<Opportunity> GetOpportunitiesForFreshProductionView(ProductType productType,
           IEnumerable<OpportunityStatus> statuses)
        {
            var opps = new Opportunities();

            if (productType == ProductType.Program)
            {
                opps.AddRange(
                    EpicenterDataContextFactory.GetDataContext()
                        .Set<Opportunity>()
                        .Include("NamedInsured")
                        .Include("NamedInsured.State")
                        .Include("PriorCoverage")
                        .Include("AgencyContact.Parent")
                        .Include("AgencyContact.Parent.Diaries")
                        .Include("AgencyContact.Parent.ProducerDataSet")
                        .Include("Diaries")
                        .Include("OppTracking")
                        .Include("Coverages")
                        .Include("Insured")
                        .Where(
                            o =>
                                o.ProductType == productType &&
                                statuses.Contains(o.Status) &&
                                o.ExpiringPolicyNumber == null &&
                                o.OppTracking.AgencyStatus == "n" ||
                                o.OppTracking.AgencyStatus == "b" ||
                                o.OppTracking.AgencyStatus == "f" ||
                                o.OppTracking.AgencyStatus == "q"))
            ;
            }
            return opps.AsQueryable();
        }


Attached is a screenshot of the error message.

and below is the code for the model of the field that is giving me all the problems:

public string DiariesIndicator
        {
            get
            {
                if (Diaries.Count > 0)
                {
                    return "glyphicon glyphicon-ok";
                }
                else if (Diaries.Count == 0)
                {
                    return "blankspace";
                }
                else if (AgencyContact.Parent.Diaries.Any(acd => acd.DateCreated >= DateCreated && acd.SalesRelated != null))
                {
                    return "fa fa-check";
                }
                else if (AgencyContact.Parent.Diaries == null)
                {
                    return "blankspace";
                }
                return "&nbsp;";
            }
        }


it is true that some of the values will be null so i have returned with a replacement value. however, this object reference not set to an instance of an object is beyond my expertise. Any input would be great.

Oh and just so those reading this knows when I run against my QA database and environment it runs fine with no problems. it isn't until i get to my unit tests that it is causing issues.

Thank you.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference Not set to an instance of an object and/or Value Cann

#2 emersonc12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-May 16

Re: Object Reference Not set to an instance of an object and/or Value Cann

Posted 03 May 2016 - 10:14 AM

Okay figured out the first error now its back to Value cannot be null. -.-
Was This Post Helpful? 0
  • +
  • -

#3 kiasta   User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 287
  • Joined: 18-November 07

Re: Object Reference Not set to an instance of an object and/or Value Cann

Posted 13 June 2016 - 02:45 PM

The only thing I can suggest is to set a debugger and check the data that is incoming. Check which property is null that you expect to have data. Linq is quirky and requires data in some cases. Also could you post your error message in full and show what you did to fix the first error as that might have an impact for your second error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1