NullReferenceException when querying projection

17 views
Skip to first unread message

Ivan Montilla

unread,
Nov 5, 2019, 10:28:51 AM11/5/19
to RavenDB - 2nd generation document database
Hello:

I have this query:

var defaultCountry = await _dSession.Query<Country>()
    .Select(x => new CountryModel(x.Code, x.Name, x.Region))
    .FirstOrDefaultAsync(cancellationToken);

With this query, I get NullReferenceException, but if I remove the Select() call, the query run.

This is the stacktrace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Raven.Client.Documents.Linq.RavenQueryProviderProcessor`1.VisitSelect(Expression operand) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProviderProcessor.cs:line 2156
   at Raven.Client.Documents.Linq.RavenQueryProviderProcessor`1.VisitQueryableMethodCall(MethodCallExpression expression) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProviderProcessor.cs:line 1585
   at Raven.Client.Documents.Linq.RavenQueryProviderProcessor`1.VisitMethodCall(MethodCallExpression expression, Boolean negated) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProviderProcessor.cs:line 1013
   at Raven.Client.Documents.Linq.RavenQueryProviderProcessor`1.VisitExpression(Expression expression) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProviderProcessor.cs:line 197
   at Raven.Client.Documents.Linq.RavenQueryProviderProcessor`1.GetAsyncDocumentQueryFor(Expression expression, Action`1 customization) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProviderProcessor.cs:line 3217
   at Raven.Client.Documents.Linq.RavenQueryProvider`1.ToAsyncDocumentQuery[TResult](Expression expression) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\Linq\RavenQueryProvider.cs:line 254
   at Raven.Client.Documents.LinqExtensions.FirstOrDefaultAsync[TSource](IQueryable`1 source, CancellationToken token) in C:\Builds\RavenDB-Stable-4.2\42021\src\Raven.Client\Documents\LinqExtensions.cs:line 611

I tried to follow the execution with the debugger and I this if block:

if (newExpression.Arguments[index] is MemberExpression member && HasComputation(member) == false)
{
    AddToFieldsToFetch(GetSelectPathOrConstantValue(member), GetSelectPath(newExpression.Members[index]));
    continue;
}
https://github.com/ravendb/ravendb/blob/a1775781322e808fd15230f296733b3d9c809ad4/src/Raven.Client/Documents/Linq/RavenQueryProviderProcessor.cs#L2173

In my debugger, newExpression.Arguments[index] is null, so isn't a MemberExpression, but is entering in the if (WTF?) and when GetSelectPath(newExpression.Members[index]) is called with a null parameter, I get a NullPointerException.

Any idea?

Oren Eini (Ayende Rahien)

unread,
Nov 5, 2019, 11:28:45 AM11/5/19
to ravendb
Can you create a test for this?
We need to see the whole thing

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/c166df98-614b-4492-be16-05543f8bdc72%40googlegroups.com.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net

Grisha Kotler

unread,
Nov 5, 2019, 12:34:14 PM11/5/19
to rav...@googlegroups.com
var defaultCountry = await _dSession.Query<Country>()
    .Select(x => new CountryModel {
         Code = x.Code,
         Name = x.Name,
         Region = x.Region
    })
    .FirstOrDefaultAsync(cancellationToken);

You can track the status of this issue here:

Grisha Kotler
Team Leader  /   Hibernating Rhinos LTD
Skype:  grisha.kotler
Support:  sup...@ravendb.net


Ivan Montilla

unread,
Nov 5, 2019, 12:38:59 PM11/5/19
to RavenDB - 2nd generation document database
Thanks Grisha, I solved it doing these, using the setters instead the constructor.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages