Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1083

Null reference when serialize logical condition with null

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4.1
    • Affects Version/s: 1.9.2
    • Component/s: Linq, Serialization
    • None
    • Environment:
      Windows 8.1, ASP NET MVC

      Appears "Object reference not set to an instance of an object.
         at MongoDB.Bson.Serialization.Serializers.Int32Serializer.Serialize(BsonWriter bsonWriter, Type nominalType, Object value, IBsonSerializationOptions options)
         at MongoDB.Bson.Serialization.BsonSerializationInfo.SerializeValue(Object value)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildComparisonQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildOrElseQuery(BinaryExpression binaryExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildOrElseQuery(BinaryExpression binaryExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildOrElseQuery(BinaryExpression binaryExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildAnyQuery(MethodCallExpression methodCallExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildMethodCallQuery(MethodCallExpression methodCallExpression)
         at MongoDB.Driver.Linq.PredicateTranslator.BuildQuery(Expression expression)
         at MongoDB.Driver.Linq.SelectQuery.Execute()
         at MongoDB.Driver.Linq.MongoQueryable`1.GetEnumerator()
         at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
         at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)"
      

      when try to serialalize following query:

      var result = _users.Where(u =>
                              u.Profile.AttendedSchools.Any(y =>
                                  x.Institution.Id == y.Institution.Id // same school
                                  // very long expression for all kinds of year intersection
                                  && (
                                      (!x.ToPresent && !y.ToPresent && x.FromYear <= y.ToYear && y.FromYear <= x.ToYear) // to present is not set
                                      || (x.ToPresent && !y.ToPresent && x.FromYear <= y.ToYear) // first guy is in school now
                                      || (!x.ToPresent && y.ToPresent && y.FromYear <= x.ToYear) // second guy is in school now
                                      || (x.ToPresent && y.ToPresent) // both is in school now
                                  )
      

      where x - object in memory.

      Core problem is x.ToYear - it is Nullable<int>, and if it null, exception occurs. Such comparsion is OK for C# code due to http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tsdaemon Anatoliy Stegniy
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: