Null reference exception

MattNorman

Well-known member
Joined
May 22, 2021
Messages
98
Programming Experience
1-3
I am getting an object null reference when setting a property on a newly created object:
C#:
ReportAgentCallSummaryFilterModel filtersReportPeriod = new ReportAgentCallSummaryFilterModel();
filtersReportPeriod.ReportInterval = FilterData.ReportInterval;

The exception is thrown on the second line and exception message is as follows:
System.NullReferenceException: Object reference not set to an instance of an object.
at ApexCCM.Models.ReportAgentCallSummaryFilterModel.set_ReportInterval(ReportIntervals value) in C:\Users\normanm\source\repos\New\ApexCCM\Models\ReportAgentCallSummaryFilterModel.cs:line 65
at ApexCCM.ViewModels.ReportOperationalInsightsViewModel.GetReportDataAgent() in C:\Users\normanm\source\repos\New\ApexCCM\ViewModels\ReportOperationalInsightsViewModel.cs:line 436
I am a little confused as inspecting both 'filtersReportPeriod' and 'FilterData' shows that neither are null.

I also added in to test lines that don't throw any exceptions as follows:

C#:
ReportAgentCallSummaryFilterModel filtersReportPeriod = new ReportAgentCallSummaryFilterModel();
ReportIntervals test = FilterData.ReportInterval;
ReportIntervals test2 = filtersReportPeriod.ReportInterval;
filtersReportPeriod.ReportInterval = FilterData.ReportInterval;

The property for the 'ReportAgentCallSummaryFilterModel' class is as follows:
C#:
private ReportIntervals reportInterval = ReportIntervals.Intraday;

public ReportIntervals ReportInterval
{
    get
    {
        return reportInterval;
    }
    set
    {
        reportInterval = value;
        ReportTimesRowHeight = ReportInterval == ReportIntervals.Intraday ? 35 : 0;
        AppDataStore.ReportOperationalInsightsFilterViewModelRef.GetDates();
    }
}
 
I am getting an object null reference when setting a property on a newly created object:
C#:
ReportAgentCallSummaryFilterModel filtersReportPeriod = new ReportAgentCallSummaryFilterModel();
filtersReportPeriod.ReportInterval = FilterData.ReportInterval;

The exception is thrown on the second line and exception message is as follows:

I am a little confused as inspecting both 'filtersReportPeriod' and 'FilterData' shows that neither are null.

I also added in to test lines that don't throw any exceptions as follows:

C#:
ReportAgentCallSummaryFilterModel filtersReportPeriod = new ReportAgentCallSummaryFilterModel();
ReportIntervals test = FilterData.ReportInterval;
ReportIntervals test2 = filtersReportPeriod.ReportInterval;
filtersReportPeriod.ReportInterval = FilterData.ReportInterval;

The property for the 'ReportAgentCallSummaryFilterModel' class is as follows:
C#:
private ReportIntervals reportInterval = ReportIntervals.Intraday;

public ReportIntervals ReportInterval
{
    get
    {
        return reportInterval;
    }
    set
    {
        reportInterval = value;
        ReportTimesRowHeight = ReportInterval == ReportIntervals.Intraday ? 35 : 0;
        AppDataStore.ReportOperationalInsightsFilterViewModelRef.GetDates();
    }
}
To add to this, adding a break point on the property setter doesn't get triggered at all so the exception is thrown before that is called even though the exception message shows the setter being called in the trace.
 
What does line 65 noted here:
C#:
C:\Users\normanm\source\repos\New\ApexCCM\Models\ReportAgentCallSummaryFilterModel.cs:line 65
correspond to in:
C#:
set
{
    reportInterval = value;
    ReportTimesRowHeight = ReportInterval == ReportIntervals.Intraday ? 35 : 0;
    AppDataStore.ReportOperationalInsightsFilterViewModelRef.GetDates();
}
If I had to guess, you likely have a race condition somewhere.
 
Thanks, found the issue on line 65. I was looking at the wrong model which i'd previously had issues with and had added a check to resolve.

Is it normal practice to catch exceptions within property setters? Seems it would have been easier to follow although the required detail was in the exception message already thrown I guess.
 
Exceptions are for exceptional error conditions. They shouldn't be used for ordinary error conditions. Getters and setters should never throw exceptions. Also depending on which programming camp you belong to, constructors (and destructors/disposers) should also never throw exceptions. At least that is if you adhere to C#'s original guiding principle of "least surprise". Unfortunately, over the past few years with everybody and their brother jumping into C#, a lot of the original C# guiding principles are getting eroded away.

So to answer your question, yes you should catch an exception within the setter -- but only if you can safely handle the exception. If the exception is something that can't be safely handled, then you should just let the exception bubble up because, well, it's an exceptional error condition.
 
Back
Top Bottom