-2

I am getting the model values to an action in controller and trying to pass the values to another function in another class by setting it to another object . In the line of invoking the second function, the object throws null exception. It doesnt show null in the if condition check either. Please find code below.

public ActionResult SearchBySecurity(SearchViewModel srchModel)
    {
       var searchDTO = new VisitorSearchDTO();
        //_mapper.Map<SearchViewModel, VisitorSearchDTO>(srchModel,searchDTO);
        searchDTO.VisitorFirstName = srchModel.VisitorFirstName;
        searchDTO.VisitorContactNumber = srchModel.VisitorContactNumber;
        searchDTO.VisitorCompany = srchModel.VisitorCompany;
        searchDTO.VisitDate = srchModel.VisitDate;
        if (searchDTO != null)
        {
            var searchResultsDTO = _searchVisitor.SearchForVisitors(searchDTO);
        }
        ModelState.Clear();
        return View("SearchBySecurity",srchModel);
Meenu
  • 27
  • 1
  • 6
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Feb 13 '17 at 09:35
  • 1
    Debug your code. We cannot do it for you. You have not even shown where you initialize `_searchVisitor` (it could be `null`). And if its not, then your `SearchForVisitors()` method is throwing the error –  Feb 13 '17 at 09:39

2 Answers2

0

I am guessing you are getting null exception when you are trying to map your models. Are you using AutoMapper? What is _mapper? If AutoMapper, then it might be worth checking your mapping profile code once if you have used any custom (before, after) mappings.

Rest as everyone has suggested, debug your code and state more clearly where the probles is, what you have tried and give clear definition of your types and environment

Jinish
  • 1,983
  • 1
  • 17
  • 22
0

Thanks Stephen and Jinish for your inputs. I have managed to solve the issue. The trouble was in initializing the controller with mapper and service parameters. Default parameter-less constructor was taken up in the code and hence mapper and service objects were not getting initialized. Solved this by proper implementation of interfaces and mapping profiles.

Meenu
  • 27
  • 1
  • 6