title: How to avoid NullPointerException in test used MockMvc?
body: <p>I have controller:</p>
  
  <pre><code>@Controller
  public class EventMenuController{
  
      @RequestMapping(value = "/updateEvent", method = RequestMethod.POST)
      public String updateEvent(Model model,
              @Valid @ModelAttribute("existedEvent") Event event,
              BindingResult result,
              @ModelAttribute("linkedCandidates") Set&lt;Candidate&gt; candidates,
              @ModelAttribute("linkedvacancies") Set&lt;Vacancy&gt; vacancies,
              @RequestParam(required = true, value = "selectedEventStatusId")Integer EventStatusId,
              @RequestParam(required = true, value = "selectedEventTypeId")Integer EventTypeId ,
              RedirectAttributes attributes) {
          if (result.hasErrors()) {
              //model.addAttribute("idEvent", event.getId());
              event.setCandidates(candidates);
              event.setVacancies(vacancies);
              return "eventDetails";
          }
          eventService.updateEventAndLinkedEntities(event, candidates, vacancies ,EventTypeId,EventStatusId);
          attributes.addAttribute("idEvent",event.getId() );//event is null therefore NPE here
          attributes.addAttribute("message", "submitted correctly at "+new Date());
          return "redirect:eventDetails";
       }
   }
  </code></pre>
  
  <p>For testing this method I wrote following class:</p>
  
  <pre><code>@ContextConfiguration(locations = { "classpath:/test/BeanConfigUI.xml" })
  @RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  public class EventMenuControllerTest {
  
  
      @Test
      public void updateEvent() throws Exception{
          MockHttpServletRequestBuilder request = MockMvcRequestBuilders
                  .post("/updateEvent");
          request.param("selectedEventStatusId", "1");
          request.param("selectedEventTypeId", "1");
  
          EventMenuController eventMenuController = (EventMenuController) wac.getBean("eventMenuController");
          EventService mockEventService = Mockito.mock(EventService.class);
          eventMenuController.eventService = mockEventService;
  
          Mockito.doNothing().when(mockEventService).updateEventAndLinkedEntities(any(Event.class), any(Set.class),any(Set.class), any(Integer.class), any(Integer.class));
  
          ResultActions result = mockMvc.perform(request);
  
          result.andExpect(MockMvcResultMatchers.view().name("redirect:eventDetails"));
          result.andExpect(MockMvcResultMatchers.model().attributeExists("idEvent"));
          result.andExpect(MockMvcResultMatchers.model().attributeExists("message"));
  
      }
  
  }
  </code></pre>
  
  <p>In the process request executing on server side I see error that show me that event object is null.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>What request I must write that pass event to server-side(controller method) using MockMvc ?</p>