asp.netif-statementrazornullreferenceexception

Nullable object must have a value in Context of Count Expression


I am receiving this error on a report page on my asp.net app. I have looked at the other answers to questions asked about this error, but am still unable to understand how to resolve it.

I am receiving the error on this line from my .cshtml page:

@(@Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00").Count() +

Here is the complete table row where this is occurring:

                            <tr>
                                <td width="16%" align="left">Count</td>
                                <td width="18%" align="right">
                                    @(@Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "01").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "02").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "03").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "04").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "05").Count())
                                </td>
                            </tr>

My .cs is as follows:

public class IndexModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public IndexModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public IEnumerable<Incident> Results { get; set; }

    public void OnGet()
    {
        Results = _db.Incident.ToList();
    }

    public void OnPost(DateTime startdate, DateTime enddate, string sortOrder)
    {
        Results = (from x in _db.Incident where (x.ArrestDate >= startdate) && (x.ArrestDate <= enddate) select x).ToList();

        var startdate1 = startdate.ToShortDateString();
        var enddate1 = enddate.ToShortDateString();

        ViewData["startingparameter"] = $"Starting Arrest Date: {startdate1}";
        ViewData["endingparameter"] = $"Ending Arrest Date: {enddate1}";

    }

}

The model Incident that this is based on has the ArrestTime set as nullable:

        [Display(Name = "Arrest Time")]
        [DataType(DataType.Time)]
        public DateTime? ArrestTime { get; set; }

So, I am not quite sure why this is happening. When I published the app, it was working, but has since stopped and the report page returns a 500 error when trying to run it in production.

I have tried adding an if(@Model.Results.Where(x => x.ArrestTime.HasValue){} and putting the Count expression inside of the {}, but that doesn't work.

Any advice would be much appreciated. Thanks so much.


Solution

  • If ArrestTime is nullable then that implies that it might not have a value. However, this code assumes that it will always have a value:

    Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00")
    

    If it always has a value then don't make it nullable, just use a DateTime. However, if it might not have a value then you would need to check if that value exists before trying to use it. For example:

    Model.Results.Where(x => x.ArrestTime.HasValue && x.ArrestTime.Value.ToString("HH") == "00")
    

    or:

    Model.Results.Where(x => x.ArrestTime != null && x.ArrestTime.Value.ToString("HH") == "00")