0

In GetAuditLogsAsync method, my code:

    var query = from auditLog in _auditLogRepository.GetAll()
            join user in _userRepository.GetAll() on auditLog.UserId equals user.Id into userJoin
            from joinedUser in userJoin.DefaultIfEmpty()
            select new {AuditLog = auditLog, joinedUser.NickName};
var count = await query.CountAsync();

When I did a unit test, an error occurred:

    System.NullReferenceException : Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at lambda_method(Closure , QueryContext )
   at Microsoft.EntityFrameworkCore.Storage.Internal.InMemoryDatabase.<>c__DisplayClass8_0`1.<CompileAsyncQuery>b__0(QueryContext qc)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at ChiakiYu.AuditLogs.AuditLogAppService.<GetAuditLogsAsync>d__3.MoveNext() in G:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\src\ChiakiYu.Application\AuditLogs\AuditLogAppService.cs:line 64
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ChiakiYu.Tests.Auditing.AuditLogAppService_Tests.<Should_Get_Audit_Logs>d__2.MoveNext() in G:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\test\ChiakiYu.Tests\Auditing\AuditLogAppService_Tests.cs:line 75
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

But when i modify the code:

var query = from auditLog in _auditLogRepository.GetAll()
            select new {AuditLog = auditLog};
var count = await query.CountAsync();

it is ok.

Why an error occurs after using join

What should I do ?Thanks.

ChiakiYu
  • 685
  • 1
  • 7
  • 7
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – EJoshuaS - Reinstate Monica May 16 '17 at 21:11

1 Answers1

0

I know, I'm so stupid. When the AuditLog.UserId is null, joinedUser is null, joinedUser.NickName is written incorrectly.

ChiakiYu
  • 685
  • 1
  • 7
  • 7