This is my datatables serverside implementation. FilterInput contains 5 variables:
- Level <- string
- Message <- string
- Exception <-string
- StartDate <- DateTime
- EndDate <- DateTime
For some reason when I run this code as it is, I will always get this error:
{System.NullReferenceException: Object reference not set to an instance of an object.
This is referring to this line:
data = data.Where(
                        u => u.Level.ToString().ToLower().Contains(FilterInput.Level.ToLower()) &&
                        u.Message.ToString().ToLower().Contains(FilterInput.Message.ToLower()) &&
                        u.Exception.ToString().ToLower().Contains(FilterInput.Exception.ToLower())
                    ).ToList();
However, if I remove the search for FilterInput.Exception, everything runs fine again. I have tested it with input ("abc") or without input ("") and the results are the same. The other FilterInputs don't have the same error.
public JsonResult Search(SearchViewModels Input, EventLogsSearchViewModel FilterInput)
{
    JsonResult result = new JsonResult(null);
    try
    {
        var data = dbContext.EventLogs.ToList();
        int totalRecords = data.Count;
        var modelStructure = new Dictionary<int, string>();
        modelStructure.Add(1, "Level");
        modelStructure.Add(2, "TimeStamp");
        modelStructure.Add(3, "LogEvent");
        modelStructure.Add(4, "Message");
        modelStructure.Add(5, "MessageTemplate");
        modelStructure.Add(6, "Exception");
        modelStructure.Add(7, "Properties");
        var StartDate = FilterInput.StartDate != default(DateTime);
        var EndDate = FilterInput.EndDate != default(DateTime);
        if ((!string.IsNullOrEmpty(FilterInput.Level) && !string.IsNullOrWhiteSpace(FilterInput.Level)) ||
            (!string.IsNullOrEmpty(FilterInput.Message) && !string.IsNullOrWhiteSpace(FilterInput.Message)) ||
            (!string.IsNullOrEmpty(FilterInput.Exception) && !string.IsNullOrWhiteSpace(FilterInput.Exception)) ||
            (StartDate && EndDate))
        {
            data = data.Where(
                u => u.Level.ToString().ToLower().Contains(FilterInput.Level.ToLower()) &&
                u.Message.ToString().ToLower().Contains(FilterInput.Message.ToLower()) &&
                u.Exception.ToString().ToLower().Contains(FilterInput.Exception.ToLower())
            ).ToList();
            data = data.Where(u => u.TimeStamp >= FilterInput.StartDate && u.TimeStamp <= FilterInput.EndDate).ToList();
        }
        if (!(string.IsNullOrEmpty(Input.Order) && string.IsNullOrEmpty(Input.OrderDir)))
        {
            var columnName = modelStructure.FirstOrDefault(f => f.Key == Convert.ToInt32(Input.Order));
            data = data.AsQueryable().OrderBy(columnName.Value + " " + Input.OrderDir).ToList();
        }
        int recFilter = data.Count;
        data = data.Skip(Input.StartRec).Take(Input.PageSize).ToList();
        var modifiedData = data.Select(u => new EventLogsListViewModel
        {
            Id = u.Id,
            Message = u.Message,
            MessageTemplate = u.MessageTemplate,
            Level = u.Level,
            TimeStamp = u.TimeStamp,
            Exception = u.Exception,
            Properties = u.Properties,
            LogEvent = u.LogEvent
        });
        result = this.Json(new
        {
            draw = Convert.ToInt32(Input.Draw),
            recordsTotal = totalRecords,
            recordsFiltered = recFilter,
            data = modifiedData,
            order = Input.Order,
            orderdir = Input.OrderDir
        });
    }
    catch (Exception e)
    {
        logger.LogError(e, LoggingGlobals.LoadingException);
    }
    return result;
}
EDIT: The exception still happens even when FilterInput.Exception is not null
