I'm getting a strange NullReferenceException on a simple query with Dataobjects 4.4 (latest stable build)

Code:

  var query = Query.All<Document>()
                .Where(doc => doc.Container == TrashContainer)
                .Take(thresholdMany);

Exception:

System.NullReferenceException occurred
  Message=Object reference not set to an instance of an object.
  Source=Xtensive.Orm
  StackTrace:
       at Xtensive.Storage.Providers.Sql.BatchingCommandProcessor.ExecuteBatch(Int32 numberOfTasks, QueryRequest lastRequest)
       at Xtensive.Storage.Providers.Sql.BatchingCommandProcessor.ExecuteRequestsWithReader(QueryRequest request)
       at Xtensive.Storage.Providers.Sql.SessionHandler.<Xtensive.Storage.Providers.Sql.IQueryExecutor.ExecuteTupleReader>d__a.MoveNext()
       at Xtensive.Storage.Providers.Sql.SqlProvider.<OnEnumerate>d__0.MoveNext()
       at Xtensive.Storage.Rse.Providers.ExecutableProvider.<GetEnumerator>d__0.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Xtensive.Storage.Rse.RecordSet.<GetGreedyEnumerator>d__0.MoveNext()
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at Xtensive.Core.EnumerableExtensions.<Batch>d__20`1.MoveNext()
       at Xtensive.Core.EnumerableExtensions.<ApplyBeforeAndAfter>d__28`1.MoveNext()
       at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
       at lambda_method(Closure , Object[] , IEnumerable`1 , Session , Dictionary`2 , ParameterContext )
       at Xtensive.Core.DelegateBindExtensions.<>c__DisplayClass1f`6.<Bind>b__1e(T2 arg2, T3 arg3, T4 arg4, T5 arg5)
       at Xtensive.Orm.Linq.TranslatedQuery`1.Execute(Session session, ParameterContext parameterContext)
       at Xtensive.Orm.Linq.QueryProvider.Execute[TResult](Expression expression)
       at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
       at Business.Tools.EmptyTrash.Process()

Workaround:

 Container trash = TrashContainer;
  var query = Query.All<Document>()
                .Where(doc => doc.Container == trash)
                .Take(thresholdMany);

asked May 27 '11 at 06:14

olorin's gravatar image

olorin
358848792


One Answer:

Hello olorin,

Looks like this is a closure. As the query execution is lazy, the value of TrashContainer parameter is being calculated right before the execution, at that moment the value of the variable named TrashContainer is null.

To test the approach, try executing the query immediately, like this:

var query = Query.All<Document>()
            .Where(doc => doc.Container == TrashContainer)
            .Take(thresholdMany)
            .ToList();

This time you won't get any NullReferenceException.

answered May 27 '11 at 06:28

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA