question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.NullReferenceException on navigational property (2.0)

See original GitHub issue

This query:

var resultWorks = await this.context.Carts
                .Where(t => t.OwnerId == this.LoggedInUserId)
                .Select(t => new
                {
                    something = t.Configuration != null && t.Configuration.Parameters != null ? t.Configuration.Parameters.ToArray() : null
                })
                .ToArrayAsync();

Entity framework class:

//Partial DbCart:
        public int? ConfigurationId { get; set; }
        public DbConfiguration Configuration { get; set; }

//Partial DbConfiguration 
public List<DbConfigurationParameter> Parameters { get; set; }

throws error

Exception message: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Stack trace:
Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler+<_IncludeAsync>d__18.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor+<_ExecuteAsync>d__8.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+AsyncSelectEnumerable+AsyncSelectEnumerator+<MoveNext>d__3.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor+EnumeratorExceptionInterceptor+<MoveNext>d__5.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Collections.Generic.AsyncEnumerableHelpers+<ToArrayWithLength>d__1.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Collections.Generic.AsyncEnumerableHelpers+<ToArray>d__0.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Further technical details

EF Core version: (2.0) Database Provider: (Microsoft.EntityFrameworkCore.SqlServer) - SQL Server 2016 Operating system: Windows 10 IDE: (e.g. Visual Studio 2017)

Query is part of bigger query, which freeze entire execution of code. Step over (F10) never actually step over and web request nevery finish executing (there is no response to client, no error nothing). This worked with EF Core 1.1.2.

return await this.context.Carts
                .Where(t => t.OwnerId == this.LoggedInUserId)
                .OrderBy(t => t.Order).ThenBy(t => t.Id)
                .Select(t => new ViewCartModel()
                {
                    id = t.Id,
                    quantity = t.Quantity,
                    model = t.Title,
                    modelId = t.ModelId,
                    configurationId = t.ConfigurationId,
                    processing = false,
                    price = t.Configuration.Parameters
                        .Where(t2 => t2.Parameter.PriceType == ePriceType.Price)
                        .Select(t2 => BitConverter.ToDouble(t2.ParameterValue.Value, 0))
                        .Sum()
                })
                .ToArrayAsync();

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ivanovmv90commented, Aug 21, 2017

Looks like problem with async methods. For me, problem dissapeared after changing ToListAsync() with ToList().

Also, https://github.com/aspnet/EntityFrameworkCore/issues/9449

0reactions
ajcvickerscommented, Sep 15, 2017

@MaklaCof Thanks for the feedback; much appreciated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core NullReferenceException on Related Navigation ...
I have tried removing the virtual modifiers on the related entities navigation properties but the error persisted. Installing the NuGet package ...
Read more >
NullReferenceException Class (System)
The exception that is thrown when there is an attempt to dereference a null object reference.
Read more >
NullReferenceException Constructor (System)
Initializes a new instance of the NullReferenceException class, setting the Message property of the new instance to a system-supplied message that describes ...
Read more >
System.NullReferenceException thrown for many ...
When creating a many-to-many mapping in EF Core 5, we did not need to specify any navigation properties on the entities making up...
Read more >
Resolve Non-nullable Property Must Contain a Non-null ...
When compiling a C# application, we may see the warning “Non-nullable property must contain a non-null value when exiting constructor.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found