Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

When searching using filter System.NullReferenceException #4294


User avatar
0
joe704la created

I haven't had this issue before today. I created a new service. Very basic one. It returns a PagedResultDto. It works just fine when not using a search parameter. But as soon as I add a search parameter I get the below error.

My input looks like this. Any help would be greatly appreciated.

public class GetEmployeePagedInput : PagedAndSortedInputDto, IShouldNormalize
    {
        [StringLength(120)]
        public string Filter { get; set; }

        public void Normalize()
        {
            if (string.IsNullOrEmpty(Sorting))
            {
                Sorting = "FirstName";
            }
        }
    }

ERROR 2017-12-05 18:09:40,996 [23 ] Mvc.ExceptionHandling.AbpExceptionFilter - Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object. at lambda_method(Closure , ValueBuffer ) at System.Linq.AsyncEnumerable.WhereEnumerableAsyncIterator1.<MoveNextCore>d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.AsyncIterator1.<MoveNext>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.TakeAsyncIterator1.<MoveNextCore>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.AsyncIterator1.<MoveNext>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator2.<MoveNextCore>d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.AsyncIterator1.<MoveNext>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.<Aggregate_>d__63.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at PHS.EmployeeDirectory.EmployeeDirectoryService.


6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Can you show the implementation of EmployeeDirectoryService.<OpenEmployeeDirectory>?

  • User Avatar
    0
    joe704la created

    Sure. Its two methods. One for the actual service and the other to create the query.

    public async Task<PagedResultDto<EmployeeListDto>> OpenEmployeeDirectory(GetEmployeePagedInput input)
            {
                var query = CreateEmployeeDirectoryQuery(input);
    
                var employeeList = await query
                    .OrderBy(input.Sorting)
                    .PageBy(input)
                    .ToListAsync();
    
                var employeeCount = await query.CountAsync();
    
                return new PagedResultDto<EmployeeListDto>
                    (employeeCount, ObjectMapper.Map<List<EmployeeListDto>>(employeeList));
            }
    
            private IQueryable<Employee> CreateEmployeeDirectoryQuery(GetEmployeePagedInput input)
            {
                var query = _employeeRepository.GetAll()
                    .Select(e => new Employee
                {
                    Id = e.Id,
                    ProId = e.ProId,
                    FirstName = e.FirstName,
                    LastName = e.LastName,
                    WorkPhone = e.WorkPhone,
                    WorkExtension = e.WorkExtension,
                    EmployeeEmail = e.EmployeeEmail,
                    Department = e.Department,
                    Division = e.Division,
                    SupervisorName = e.SupervisorName
                });
    
                query = query
                    .WhereIf(!input.Filter.IsNullOrWhiteSpace(), pc => pc.FirstName.Contains(input.Filter) ||
                                                                       pc.LastName.Contains(input.Filter) ||
                                                                       pc.EmployeeStatus.Contains(input.Filter) ||
                                                                       pc.EmployeeEmail.Contains(input.Filter));
    
                return query;
            }
    
  • User Avatar
    0
    aaron created
    Support Team

    Could the FirstName, LastName, EmployeeStatus or EmployeeEmail be null?

  • User Avatar
    0
    joe704la created

    Yes. None of them are required.

  • User Avatar
    0
    aaron created
    Support Team

    Then you need to add null-checks before using Contains.

  • User Avatar
    0
    joe704la created

    Oh yes I should have thought of that. Thanks for the help.