1

The following code throws a NullReferenceException. If I filter user table using another column, exception is not throwed. DateTime type columns cause this exception.

using (MyDataContext dc = new MyDataContext())
    {
        var ids = dc.Users.Where(c => c.BirthDate < DateTime.Now.AddYears(-10)).Select(c => c.UserId);

        var list = (from c in dc.Users
                    where ids.Contains(c.UserId)
                    select c).ToList();
    }

Stack trace like this:

[NullReferenceException: Object reference not set to an instance of an object.]


System.Data.Linq.SqlClient.SqlFactory.Member(SqlExpression expr, MemberInfo member) +182
   System.Data.Linq.SqlClient.QueryConverter.VisitMemberAccess(MemberExpression ma) +260
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +939
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +1011
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitChangeType(Expression expression, Type type) +12
   System.Data.Linq.SqlClient.QueryConverter.VisitCast(UnaryExpression c) +117
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +619
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +35
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +427
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +35
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +427
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate) +125
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +3403
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +74
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector) +17
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +1368
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +74
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitContains(Expression sequence, Expression value) +377
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +9129
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +74
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
   System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
   System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate) +125
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +3403
   System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +74
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
   System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +79
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +114
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +132
   System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +35
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +7667686
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +61
   lab_test.Page_Load(Object sender, EventArgs ee) in d:\Projeler\Kardelen\Kod\Kardelen.Web.UI\lab\test.aspx.cs:543
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Is anybody get this exception?

Thanks for response.

3
  • I had some errors in .Net when using a column of Type DateTime that contained Null values. I don't know about NULL values in Linq but try running your code against a table without NULL values in that column to check. Added: Or see what happens when you "add" -10 years to DateTime.MinValue? Commented Nov 30, 2011 at 12:15
  • 1
    Not the cause of your problem, but you probably shouldn't use DateTime.Now.AddYears(-10) within the Where clause. Define it once before so it is the same for all calls. As to your problem, I'm guessing BirthDate is simply null? Commented Nov 30, 2011 at 12:26
  • Steven Jeuris, when I declare DateTime.Now.AddYears(-10) before selection as a variable, the problem is solved. Thanks for reply. Commented Nov 30, 2011 at 12:32

2 Answers 2

5

If I filter user table using another column, exception is not throwed. DateTime type columns cause this exception.

Assuming you mean this comparison: c.BirthDate < DateTime.Now.AddYears(-10)

Most likely BirthDate is null. In case it is sufficient in your scenario to just skip those users, you can add a check to see whether it is null before doing the date comparison.

As a sidenote, you should probably define DateTime.Now.AddYears(-10) once before calling Where to prevent it from being recalculated, and worse from the value changing over time.

DateTime tenYearsAgo = DateTime.Now.AddYears(-10);
var ids = dc.Users
    .Where(c => c.BirthDate != null && c.BirthDate < tenYearsAgo)
    .Select(c => c.UserId);
Sign up to request clarification or add additional context in comments.

Comments

1

Most likely your c.BirthDate property is nullable , DateTime? BirthDate.

That would match an Allow-NULL column in your table.

A simple check should be enough:

 dc.Users.Where(c => c.BirthDate.HasValue 
       && (c.BirthDate < DateTime.Now.AddYears(-10))
 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.