title: Request processing failed; nested exception is java.lang.NullPointerException with root cause java.lang.NullPointerException
body: <p>Here is a brief description of what I am doing.</p>
  
  <p>The application has the form elements in JSP which redirects to the Controller class after pressing submit. The controller calls the service layer which in turn calls DAO and finally adds the records to the data base. The issue I am having is that in the Controller class, I can receive the request parameters (POST) but the thing is that there is an issue while calling the <code>addStudent()</code> of the service layer. The issue is with the Null Pointer Exception on object. Could you guys review the code and provide me suggestions?</p>
  
  <p>Here is the full Stacktrace, throwing errors with pointing the object, i.s <code>nullPointerException</code>.</p>
  
  <pre><code>INFO: Server startup in 4385 ms
  Apr 28, 2014 11:45:38 AM org.apache.catalina.core.StandardWrapperValve invoke
              SEVERE: Servlet.service() for servlet [springDispatcher] in context with path [/StudentManagementSystem] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
              java.lang.NullPointerException
             at com.vastika.controllers.SpringController.addStudent(SpringController.java:42)
                  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                  at java.lang.reflect.Method.invoke(Unknown Source)
                  at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
                  at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
                  at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
                  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
                  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
                  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
                  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
                  at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
                  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
                  at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
                  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
                  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
                  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
                  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
                  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
                  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
                  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
                  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
                  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
                  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
                  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
                  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
                  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
                  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
                  at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
                  at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>Here are the codes</p>
  
  <p><strong>SpringController class</strong></p>
  
  <pre><code>    package com.vastika.controllers;
  
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RequestParam;
  
      import com.vastika.student.model.StudentModel;
      import com.vastika.student.services.StudentServiceImpl;
  
      @Controller
      @RequestMapping("/students/*")
      public class SpringController {
  
  
          private StudentServiceImpl studentService;
  
          public void setStudentService(StudentServiceImpl studentService) {
              this.studentService = studentService;
          }
  
          @RequestMapping(value = "/students/addstud", method = RequestMethod.POST)
  
  
          public String addStudent(@RequestParam(value = "name") String name,
                  @RequestParam(value = "age") String age,
                  @RequestParam(value = "address") String address,
                  @RequestParam(value = "email") String email) {
  
              StudentModel student = new StudentModel();
  
              student.setName(name);
              student.setAge(age);
              student.setAddress(address);
              student.setEmail(email);        
  
              if (studentService.addStudentService(student)) {
                  return "Student is added to the database";
              }
              return "Student is not added to the database";
  
          }
  
  
  
      }
  </code></pre>
  
  <p><strong>StudentServiceImpl Class</strong></p>
  
  <pre><code>    package com.vastika.student.services;
  
      import java.util.List;
  
      import com.vastika.student.dao.StudentDaoImpl;
      import com.vastika.student.model.StudentModel;
  
      public class StudentServiceImpl implements IStudentService {
  
  
          private StudentDaoImpl studentDao;
  
          public void setStudentDao(StudentDaoImpl studentDao) {
              this.studentDao = studentDao;
          }
  
          @Override
          public boolean addStudentService(StudentModel student) {
              if(studentDao.addStudentDao(student)){
                  return true;            
              }       
              return false;
              }
  
          @Override
          public boolean delStudentService(int id) {
              // TODO Auto-generated method stub
              return false;
          }
  
          @Override
          public boolean updateStudentService(StudentModel student) {
              // TODO Auto-generated method stub
              return false;
          }
  
          @Override
          public boolean getStudentByIdService(int id) {
              // TODO Auto-generated method stub
              return false;
          }
  
          @Override
          public List&lt;StudentModel&gt; getAllStudentsService() {
              // TODO Auto-generated method stub
              return null;
          }
  
      }
  </code></pre>
  
  <p><strong>Model Class:</strong></p>
  
  <pre><code>    package com.vastika.student.model;
  
      public class StudentModel {
  
          private int studentId;
          private String name;
          private String age;
          private String address;
          private String email;
  
          public int getStudentId() {
              return studentId;
          }
          public void setStudentId(int studentId) {
              this.studentId = studentId;
          }
          public String getName() {
              return name;
          }
          public void setName(String name) {
              this.name = name;
          }
  
          public String getAddress() {
              return address;
          }
          public void setAddress(String address) {
              this.address = address;
          }
          public String getEmail() {
              return email;
          }
          public void setEmail(String email) {
              this.email = email;
          }
          public String getAge() {
              return age;
          }
          public void setAge(String age) {
              this.age = age;
          }
  
      }
  </code></pre>
  
  <p><strong>spring.xml file</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;bean id="student" class="com.vastika.student.model.StudentModel"/&gt;
  
  
      &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt;
  
  
      &lt;bean id="studentService" class="com.vastika.student.services.StudentServiceImpl"&gt;
      &lt;property name="studentDao" ref="studentDao"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="studentDao" class="com.vastika.student.dao.StudentDaoImpl"/&gt;
  
      &lt;bean id="studentController" class="com.vastika.controllers.SpringController"&gt;
      &lt;property name="studentService" ref="studentService"&gt;&lt;/property&gt;
      &lt;/bean&gt; 
  
      &lt;/beans&gt;
  </code></pre>
  
  <p><strong>springdispatcher-servlet.xml file</strong></p>
  
  <pre><code>    &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
         http://www.springframework.org/schema/beans     
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
          &lt;context:annotation-config /&gt; 
        &lt;context:component-scan base-package="com.vastika.controllers" /&gt; 
  
      &lt;!--   &lt;bean id="studentService" class="com.vastika.student.services.StudentServiceImpl"&gt;
      &lt;property name="studentDao" ref="studentDao"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="studentDao" class="com.vastika.student.dao.StudentDaoImpl"/&gt;
  
      &lt;bean id="studentModel" class="com.vastika.student.model.StudentModel"/&gt; --&gt;
  
  
        &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
            &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
            &lt;property name="suffix" value=".jsp" /&gt;
         &lt;/bean&gt;
  
      &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml file</strong></p>
  
  <pre><code>    &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
                version="2.4"&gt;
  
        &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
        &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
  
        &lt;context-param&gt;
       &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
       &lt;param-value&gt;/WEB-INF/springDispatcher-servlet.xml;classpath:spring.xml&lt;/param-value&gt;
       &lt;/context-param&gt;
  
  
         &lt;servlet&gt;
            &lt;servlet-name&gt;springDispatcher&lt;/servlet-name&gt;
            &lt;servlet-class&gt;
               org.springframework.web.servlet.DispatcherServlet
            &lt;/servlet-class&gt;
            &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
         &lt;/servlet&gt;
  
          &lt;servlet-mapping&gt;
            &lt;servlet-name&gt;springDispatcher&lt;/servlet-name&gt;
            &lt;url-pattern&gt;/&lt;/url-pattern&gt;
         &lt;/servlet-mapping&gt;
  
      &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt; 
      &lt;!--    
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.request.RequestContextListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
           --&gt;
  
      &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>index.jsp file</strong></p>
  
  <pre><code>    &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
                version="2.4"&gt;
  
        &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
        &lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
        &lt;/welcome-file-list&gt;
  
        &lt;context-param&gt;
       &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
       &lt;param-value&gt;/WEB-INF/springDispatcher-servlet.xml;classpath:spring.xml&lt;/param-value&gt;
       &lt;/context-param&gt;
  
  
         &lt;servlet&gt;
            &lt;servlet-name&gt;springDispatcher&lt;/servlet-name&gt;
            &lt;servlet-class&gt;
               org.springframework.web.servlet.DispatcherServlet
            &lt;/servlet-class&gt;
            &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
         &lt;/servlet&gt;
  
          &lt;servlet-mapping&gt;
            &lt;servlet-name&gt;springDispatcher&lt;/servlet-name&gt;
            &lt;url-pattern&gt;/&lt;/url-pattern&gt;
         &lt;/servlet-mapping&gt;
  
      &lt;listener&gt;
              &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
          &lt;/listener&gt; 
      &lt;!--    
      &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.request.RequestContextListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
           --&gt;
  
      &lt;/web-app&gt;
  </code></pre>