I have the following code:
IList<CmsUserPermissionGroup> matchingRoles = PermissionGroups.Where(r => r.JournalCode.ToLower() == journalCode.ToLower())
.Where(r => r.CmsRole.ToLower() == role.ToLower())
.Where(r => r.AccessLevel > 0)
that I assumed would return an empty List if no results are returned. What is actually returned is the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Am I misunderstanding something? What other alternative is there?
If there was no problem with the query, an empty enumeration would be returned.
The problem here is inside your
Where()statements. EitherJournalCodeorCmsRoleare null (assuming you already checkedjournalCodeandrolefor null values somewhere else) on one of the objects inPermissionGroups. When you callToLower()on that value, it throws the above Exception.You could protect yourself a little better with the following: