title: Spring MVC "Request processing failed" 500 error
body: <p>I am new at Spring and trying to learn with tutorial. And I have a problem. I converted to xml based config to annotation based config. <a href="http://www.journaldev.com/3531/spring-mvc-hibernate-mysql-integration-crud-example-tutorial" rel="nofollow noreferrer">this</a> is tutorial.</p>
  
  <p>And I receive HTTP 500 error. This is full stacktrace.</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  
  
  root cause
  
     java.lang.NullPointerException   com.ulotrix.spring.service.PersonServiceImpl.listPersons(PersonServiceImpl.java:35) 
      com.ulotrix.spring.controller.PersonController.listPersons(PersonController.java:28)    
  
       sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  </code></pre>
  
  <p><strong>Project Structure</strong></p>
  
  <p><img src="https://i.stack.imgur.com/Fi6Ls.jpg" alt=""></p>
  
  <p><strong>AppConfig.java</strong></p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  @ComponentScan({ "com.ulotrix.spring" })
  public class AppConfig extends WebMvcConfigurerAdapter {
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }
  
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
      configurer.enable();
  }
  
  @Bean
  public SessionFactory sessionFactory() {
  
      LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
      builder.scanPackages("com.ulotrix.spring.model");
      builder.addProperties(getHibernationProperties());
  
      return builder.buildSessionFactory();
  }
  
  private Properties getHibernationProperties() {
  
      Properties prop = new Properties();
      prop.put("hibernate.format_sql", "true");
      prop.put("hibernate.show_sql", "true");
      prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  
      return prop;
  }
  
  @Bean(name = "dataSource")
  public BasicDataSource dataSource() {
  
      BasicDataSource ds = new BasicDataSource();
      ds.setDriverClassName("com.mysql.jdbc.Driver");
      ds.setUrl("jdbc:mysql://localhost:3306/deneme_db2");
      ds.setUsername("root");
      ds.setPassword("xxxx");
  
      return ds;
  }
  
  @Bean
  public HibernateTransactionManager txManger() {
      return new HibernateTransactionManager(sessionFactory());
  }
  
  @Bean
  public InternalResourceViewResolver viewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setViewClass(JstlView.class);
      viewResolver.setPrefix("/WEB-INF/views/");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
  }
  
  }
  </code></pre>
  
  <p><strong>PersonService.java</strong></p>
  
  <pre><code>public interface PersonService {
  
  public void addPerson(Person p);
  public void updatePerson(Person p);
  public List&lt;Person&gt; listPersons();
  public Person getPersonById(int id);
  public void removePerson(int id);
  
  }
  </code></pre>
  
  <p><strong>PersonServiceImpl.java</strong></p>
  
  <pre><code>@Service("personService")
  public class PersonServiceImpl implements PersonService {
  
  private PersonDAO personDAO;
  
  public void setPersonDAO(PersonDAO personDAO) {
      this.personDAO = personDAO;
  }
  
  @Override
  @Transactional
  public void addPerson(Person p) {
      this.personDAO.addPerson(p);
  }
  
  @Override
  @Transactional
  public void updatePerson(Person p) {
      this.personDAO.updatePerson(p);
  }
  
  @Override
  @Transactional
  public List&lt;Person&gt; listPersons() {
      return this.personDAO.listPersons();
  }
  
  @Override
  @Transactional
  public Person getPersonById(int id) {
      return this.personDAO.getPersonById(id);
  }
  
  @Override
  @Transactional
  public void removePerson(int id) {
      this.personDAO.removePerson(id);
  }
  }
  </code></pre>
  
  <p><strong>PersonController.java</strong></p>
  
  <pre><code>@Controller
  public class PersonController {
  
  private PersonService personService;
  
  @Autowired(required = true)
  @Qualifier(value = "personService")
  public void setPersonService(PersonService ps) {
      this.personService = ps;
  }
  
  @RequestMapping(value = "/persons", method = RequestMethod.GET)
  public String listPersons(Model model) {
      model.addAttribute("person", new Person());
      model.addAttribute("listPersons", this.personService.listPersons());
      return "person";
  }
  
  //For add and update person both
  @RequestMapping(value = "/person/add", method = RequestMethod.POST)
  public String addPerson(@ModelAttribute("person") Person p) {
  
      if(p.getId() == 0) {
          this.personService.addPerson(p);
      }else {
          this.personService.updatePerson(p);
      }
      return "redirect:/persons";
  }
  
  @RequestMapping(value = "/remove/{id}")
  public String removePerson(@PathVariable("id") int id) {
  
      this.personService.removePerson(id);
      return "redirect:/persons";
  }
  
  @RequestMapping(value = "/edit/{id}")
  public String editPerson(@PathVariable("id") int id, Model model) {
      model.addAttribute("person", this.personService.getPersonById(id));
      model.addAttribute("listPersons", this.personService.listPersons());
      return "person";
  }
  }
  </code></pre>
  
  <p><strong>PersonDAO.java</strong></p>
  
  <pre><code>public interface PersonDAO {
  
  public void addPerson(Person p);
  public void updatePerson(Person p);
  public void removePerson(int id);
  public List&lt;Person&gt; listPersons();
  public Person getPersonById(int id);
  
  }
  </code></pre>
  
  <p><strong>PersonDAOImpl.java</strong></p>
  
  <pre><code>@Component
  public class PersonDAOImpl implements PersonDAO {
  
  private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);
  
  private SessionFactory sessionFactory;
  
  public void setSessionFactory(SessionFactory sf) {
      this.sessionFactory = sf;
  }
  
  @Override
  public void addPerson(Person p) {
      Session session = this.sessionFactory.getCurrentSession();
      session.persist(p);
      logger.info("Person saved successfully, Person Details="+p);
  }
  
  @Override
  public void updatePerson(Person p) {
      Session session = this.sessionFactory.getCurrentSession();
      session.update(p);
      logger.info("Person updated successfully, Person Details="+p);
  }
  
  @SuppressWarnings("unchecked")
  @Override
  public List&lt;Person&gt; listPersons() {
      Session session = this.sessionFactory.getCurrentSession();
      List&lt;Person&gt; personList = session.createQuery("from Person").list();
      for(Person p: personList) {
          logger.info("Person List::"+p);
      }
      return personList;
  }
  
  @Override
  public Person getPersonById(int id) {
      Session session = this.sessionFactory.getCurrentSession();
      Person p = (Person) session.load(Person.class, new Integer(id));
      logger.info("Person loaded successfully, Person details="+p);
      return p;
  }
  
  @Override
  public void removePerson(int id) {
      Session session = this.sessionFactory.getCurrentSession();
      Person p = (Person) session.load(Person.class, new Integer(id));
      if(null != p) {
          session.delete(p);
      }
      logger.info("Person deleted successfully, person details="+p);
  }
  
  }
  </code></pre>
  
  <p><strong>More Exception</strong></p>
  
  <pre><code> exception
  org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>