1
var exercises = _repository.Exercises.Where(a =>
    userPrincipal.User != null && // false 
    (
        userPrincipal.Activated && a.PrivacyString == Privacy.PUBLIC_TO_REGISTERED_USERS.ToString() ||
        userPrincipal.User.Id == a.User.Id && a.PrivacyString == Privacy.PRIVATE.ToString()
    )
).ToList();

This is my code, this should never go beyond from false, but it is running into the bottom clause also. There was basically a null check, but now EF is giving me nullpointer error

{"Non-static method requires a target."}

When I remove userPrincipal.User.Id from the query, then I do not get exception, so I know that userPrincipal.User is null.

EDIT:

_repository definition.

public DbSet<Exercise> Exercises { get; set; }
IQueryable<Exercise> IRepository.Exercises {
        get { return Exercises; }
}

EDIT: query generated by EF.

SELECT 
1 AS [C1], 
CAST(NULL AS int) AS [C2], 
CAST(NULL AS varchar(1)) AS [C3], 
CAST(NULL AS varchar(1)) AS [C4], 
CAST(NULL AS varchar(1)) AS [C5], 
CAST(NULL AS varchar(1)) AS [C6], 
CAST(NULL AS varchar(1)) AS [C7], 
CAST(NULL AS int) AS [C8]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0
Jaanus
  • 16,161
  • 49
  • 147
  • 202

1 Answers1

1

The null pointer error is a result of the retrieval of the value of property on the userPrincipal variable where the variable is a null reference.

EF translates the query into SQL and also converts all used in-memory values into sql parameters. When you for example use userPrincipal.Activated in your query, then EF retrieves the PropertyInfo (or FieldInfo or MethodInfo) from the expression tree, and uses this to retrieve the value from the object. To do this you need a reference to the object that contains the value. If that object is a null-reference, you get the exception that you see.

In short: the exception is thrown because EF cannot retrieve the value for properties if your used object is a null reference.

See also: https://stackoverflow.com/a/13717781/261050

Final solution that works, doing null check outside query:

var exercises = _repository.Exercises;

if (userPrincipal.User == null) {
    exercises = exercises.Where(a => a.PrivacyString == Privacy.PUBLIC.ToString());
} else {
    exercises = exercises.Where(a => 
        a.PrivacyString == Privacy.PUBLIC.ToString() ||
        userPrincipal.Activated && a.PrivacyString == Privacy.PUBLIC_TO_REGISTERED_USERS.ToString() ||
        userPrincipal.User.Id == a.User.Id && a.PrivacyString == Privacy.PRIVATE.ToString());
}
Community
  • 1
  • 1
Maarten
  • 22,527
  • 3
  • 47
  • 68
  • Shouldn´t null check help out here? So it will not convert everything to SQL, if I do null check beforehand? – Jaanus Apr 11 '15 at 14:00
  • You should not include the null check in your query. Retrieve the values you need beforehand, check for null references, store them in local variables which should be value types (int, string, etc), and then use them in your query. Then you are never using a null reference in your query. – Maarten Apr 11 '15 at 14:02
  • Okay thanks, this helped. I never knew that I can´t check null values for external objects like that. Posted final solution in your answer. – Jaanus Apr 11 '15 at 14:07
  • The questions is not same as the one you provided, because I am actually doing null check for the object inside query, while that user, did not. Notice `userPrincipal.User != null &&` . – Jaanus Apr 11 '15 at 14:26
  • @Jaanus - Expressions are not evaluated when they are expressed... They are converted into Expression Trees and then evaluated. The problem here is that the exception occurs when the variable is being converted to the expression tree, so the Null check has not actually occurred in the expression. That's why it has to be done outside of it. – Erik Funkenbusch Apr 11 '15 at 15:32