Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illogical NullReferenceException

Tags:

c#

So in my normal course of getting some work done, i came across a generic System.NullReferenceException: 'Object reference not set to an instance of an object.'.

So no problem i'm thinking as I start looking for the culprit. After much debugging, I threw my hands up in the air and wanted to give up for the moment... My strategy for bypassing this was to just instantiate an array instead of calling some service for the data.

So i changed:

var data = service.GetData(someId);

to

var data = new DataType[]{};

to get my code compiling and debuggable so i can come back to this.

The kicker: upon doing so, i'm still getting the exception on that line. So JUST an array instantiation is yielding this exception. The exception has zero data to it. No inner exception or stack trace beyond pointing to this line. The class itself is just a poco with no methods (no constructor either) so nothing is happening internally. (see below for the class)

My next thought is that clearly, the code i'm seeing isn't what my debug session is using.

To test this thought I've

  • Cleaned in VS
  • manually deleted the bin
  • manually deleted obj
  • removed all package directories
  • restarted my computer
  • tried it in the new beta of VS
  • tried it in JetBrains' Rider

still i'm facing this.

Anyone have thoughts on how i can get more details on why this is happening, or is there something else I should be cleaning?

I've also looked at the output of git clean -xdn to see what kind of ephemeral stuff is hanging around that could be removed and nothing of interest there...

no matter what code is actually on the line, if i completely comment out this line and have completely different code there in it's place (as initially described), then that code throws the exception instead. i just had one of my colleagues grab this branch and run it and it was fine for them

3 hours deep so far... no fun

Edit:

as requested, here's a definition:

using System;
using Dapper.Contrib.Extensions;

namespace some_namespace
{
#pragma warning disable 1591
    [Table ("CompanyOptions")]
    public class CompanyOption
    {
        [ExplicitKey]
        public Guid PKID { get; set; }
        public Guid CompanyPKId { get; set; }
        public string OptionName { get; set; }
        public string OptionValue { get; set; }
        public DateTime CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? UpdatedOn { get; set; }
        public string UpdatedBy { get; set; }
    }
}

enter image description here

like image 371
MrTristan Avatar asked Nov 06 '22 19:11

MrTristan


1 Answers

So it seems there were issues with my installation of some microsoft packages. I found that running Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r in the package management console to reinstall solved my problem. In looking at the diff that that yielded, the only meaningful thing is the following target framework bumps in my packages.config:

enter image description here

These package references haven't changed recently and the project has been targeting 4.7.1 for quite some time now so not sure why the sudden problem. Seems something became corrupted in the roslyn layer perhaps?

Going to slowly back away from my computer now.

like image 162
MrTristan Avatar answered Nov 15 '22 12:11

MrTristan