title: Spring and PostgreSQL throws java.lang.NullPointerException
body: <p>I'm using Spring, Hibernate and PostgreSQL . I want to save the object in database which comes from jsp page. But it throws NullPointerException. I use sequence generator for auto ncrement and created a sequence called <strong>emp_id_seq</strong> in PostgreSQL . I print the object for finding the problem, it shows</p>
  
  <p><code>Employee [id=null, firstName=vara, lastName=raj, address=abc, dob=Tue Jun 13 00:00:00 IST 2017, nic=154184125V, email=abc@xyz.com, companyId=1, departmentId=4]</code></p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  Root Cause
  
  java.lang.NullPointerException
      org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:39)
      org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:17)
      org.postgresql.util.LruCache.borrow(LruCache.java:115)
      org.postgresql.core.QueryExecutorBase.borrowQuery(QueryExecutorBase.java:266)
      org.postgresql.jdbc.PgConnection.borrowQuery(PgConnection.java:143)
      org.postgresql.jdbc.PgPreparedStatement.&lt;init&gt;(PgPreparedStatement.java:88)
      org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:1256)
      org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:1622)
      org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:415)
      org.apache.commons.dbcp2.DelegatingConnection.prepareStatement(DelegatingConnection.java:293)
      org.apache.commons.dbcp2.DelegatingConnection.prepareStatement(DelegatingConnection.java:293)
      org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
      org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
      org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:72)
      org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:114)
      org.hibernate.id.SequenceHiLoGenerator.generate(SequenceHiLoGenerator.java:62)
      org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
      org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
      org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
      org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:97)
      org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
      org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:648)
      org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:640)
      org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:635)
      com.pulseBeatMaster.dao.impl.EmployeeDaoImpl.saveOrUpdate(EmployeeDaoImpl.java:43)
      com.pulseBeatMaster.dao.impl.EmployeeDaoImpl$$FastClassBySpringCGLIB$$dadeda1c.invoke(&lt;generated&gt;)
      org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
      org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
      com.pulseBeatMaster.dao.impl.EmployeeDaoImpl$$EnhancerBySpringCGLIB$$a0d1e9e8.saveOrUpdate(&lt;generated&gt;)
      com.pulseBeatMaster.service.impl.EmployeeServiceImpl.saveOrUpdate(EmployeeServiceImpl.java:34)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:498)
      org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
      org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
      org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
      org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
      com.sun.proxy.$Proxy64.saveOrUpdate(Unknown Source)
      com.pulseBeatMaster.controller.EmployeeController.saveUser(EmployeeController.java:60)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:498)
      org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p><strong>Model class</strong></p>
  
  <pre><code>@Entity
  @Table(name = "employee", schema = "schpulsebeat")
  public class Employee {
      private Integer id;
      private String firstName;
      private String lastName;
      private String address;
      private Date dob;
      private String nic;
      private String email;
      private Integer companyId;
      private Integer departmentId;
  
      public Employee() {
          super();
      }
  
      public Employee(Integer id, String firstName, String lastName, String address, Date dob, String nic, String email,
              Integer companyId, Integer departmentId) {
          super();
          this.id = id;
          this.firstName = firstName;
          this.lastName = lastName;
          this.address = address;
          this.dob = dob;
          this.nic = nic;
          this.email = email;
          this.companyId = companyId;
          this.departmentId = departmentId;
      }
  
          @Id
  
  //schpulsebeat is a schema. employee_id_seq is created as sequence in postgresql.
          @SequenceGenerator(name="employee_id_seq",sequenceName="schpulsebeat.employee_id_seq",allocationSize=1)
          @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="employee_id_seq")
          @Column(name="employee_id")
          public Integer getId() {
              return id;
          }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      @Column(name="first_name")
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      @Column(name="last_name")
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
      @Column(name="address")
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      @Column(name="dob")
      @DateTimeFormat(pattern = "yyyy-MM-dd")
      public Date getDob() {
          return dob;
      }
  
      public void setDob(Date dob) {
          this.dob = dob;
      }
  
      @Column(name="nic")
      public String getNic() {
          return nic;
      }
  
      public void setNic(String nic) {
          this.nic = nic;
      }
  
      @Column(name="email")
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      @Column(name="company_id", nullable=true)
      public Integer getCompanyId() {
          return companyId;
      }
  
      public void setCompanyId(Integer companyId) {
          this.companyId = companyId;
      }
  
      @Column(name="department_id", nullable=true)
      public Integer getDepartmentId() {
          return departmentId;
      }
  
      public void setDepartmentId(Integer departmentId) {
          this.departmentId = departmentId;
      }
  
      @Override
      public String toString() {
          return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", address=" + address
                  + ", dob=" + dob + ", nic=" + nic + ", email=" + email + ", companyId=" + companyId + ", departmentId="
                  + departmentId + "]";
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
      public ModelAndView saveUser(@ModelAttribute("employeeForm") Employee employee) {
  
          //System.out.println("object data"+employee.toString());
          employeeService.saveOrUpdate(employee);
          return new ModelAndView("redirect:/");
      }
  </code></pre>
  
  <p><strong>jsp page</strong> (removed all css styles for easiness)</p>
  
  <pre><code>&lt;spring:url value="/saveEmployee" var="saveURL"&gt;&lt;/spring:url&gt;
  
  
  &lt;form:form action="${saveURL}" method="POST" modelAttribute="employeeForm"&gt;
  
      &lt;form:hidden path="id" /&gt;
  
  
          &lt;form:select class="form-control" name="companyId"&gt;
              &lt;form:option value="1"&gt;Lunah Labs&lt;/option&gt;
              &lt;form:option value="2"&gt;XYZ conf&lt;/option&gt;
              &lt;form:option value="3"&gt;New Tech Lead&lt;/option&gt;
              &lt;form:option value="4"&gt;FINWIN&lt;/option&gt;
          &lt;/form:select&gt;
  
  
          &lt;form:select class="form-control" name="departmentId"&gt;
              &lt;form:option value="4"&gt;CIS&lt;/option&gt;
              &lt;form:option value="5"&gt;Tech&lt;/option&gt;
              &lt;form:option value="6"&gt;Electronic&lt;/option&gt;
              &lt;form:option value="7"&gt;Event&lt;/option&gt;
          &lt;/form:select&gt;
  
          &lt;form:input path="firstName"/&gt;
  
          &lt;form:input path="lastName" /&gt;
  
          &lt;form:input path="address" /&gt;
  
          &lt;form:input path="dob" type="date" /&gt;
  
          &lt;form:input path="nic" /&gt;
  
          &lt;form:input path="email" /&gt; 
  
          &lt;button type="submit" &gt;Submit&lt;/button&gt;
  
  &lt;/form:form&gt;
  </code></pre>
  
  <p><strong>Im worring about</strong></p>
  
  <p><strong>1) dob field</strong></p>
  
  <p><strong>2)id which is generated</strong></p>