简体   繁体   中英

EF LINQ Query throws a System.ArgumentNullException: Value cannot be null exception

I'm getting a weird exception at var result = flow.Disciplines (line 44). What's the issue there? What value is null? How do I solve it?

System.ArgumentNullException: Value cannot be null. (Parameter 'source')
         at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
         at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
         at lambda_method261(Closure , Discipline )
         at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
         at AcademicSchedule.App
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AcademicSchedule.Application.Common.Exceptions;
using AcademicSchedule.Application.Common.Interfaces;
using AcademicSchedule.Domain.Entities;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AcademicSchedule.Application.Disciplines.Queries;

using Response = IList<DisciplineDto>;

public class GetDisciplinesByFlowIdQuery : IRequest<Response>
{
    public int FlowId { get; set; }
}

public class GetDisciplinesByFlowIdQueryHandler : IRequestHandler<GetDisciplinesByFlowIdQuery, Response>
{
    private readonly IApplicationDbContext _context;
    private readonly IMapper _mapper;

    public GetDisciplinesByFlowIdQueryHandler(IApplicationDbContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    public Task<Response> Handle(GetDisciplinesByFlowIdQuery request, CancellationToken cancellationToken)
    {
        var flow = _context.Flows
            .Include(f => f.Disciplines)
            .SingleOrDefault(x => x.Id == request.FlowId);

        if (flow == null)
        {
            throw new NotFoundException(nameof(Flow), request.FlowId);
        }

        var result = flow.Disciplines // exception here
            .AsQueryable()
            .ProjectTo<DisciplineDto>(_mapper.ConfigurationProvider)
            .ToList();

        return Task.FromResult<Response>(result);
    }
}

C# has two types of variables: Value and reference. A value type holds a value directly. A reference type holds the address of an object somewhere in memory. When a reference type does not refer to any object, that reference type is null. It is invalid and will cause an exception when you attempt to use a reference type that is null.

It's hard to tell, but my guess is that flow.Disciplines is null. This should be dead simply to check. Just set a breakpoint on this line, run the program and see what value it has.

If it is null, then it will throw an exception .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM