title: HTTP Status 500 - Request processing failed. Nested exception is java.lang.NullPointerException
body: <p>So I have little problem. I can't add informations to database because I get an error:</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
  </code></pre>
  
  <p>Here is my controller:</p>
  
  <pre><code>package pl.edu.controller;
  
  @Controller
  public class StudentAdmissionController {
  
      @RequestMapping(value = "/admissionForm.html", method = RequestMethod.GET)
      public ModelAndView getAdmissionForm() {
  
          ModelAndView model = new ModelAndView("AdmissionForm");
  
          return model;
      }
  
      @RequestMapping(value = "/submitAdmissionForm.html", method = RequestMethod.POST)
      public ModelAndView submitAdmissionForm(@RequestParam("pojemnosc") float pojemnosc,
              @RequestParam("modelSamochodu") String modelSamochodu, @RequestParam("liczbaPasazerow") int liczbaPasazerow,
              @RequestParam("liczbaDrzwi") int liczbaDrzwi) {
  
          ModelAndView model = new ModelAndView("AdmissionSuccess");
  
          Silnik silnik = new Silnik();
          silnik.setPojemnosc(pojemnosc);
          silnik.setId(1);
  
          Samochod samochod = new Samochod();
          samochod.setSilnik(silnik);
          samochod.setLiczbaDrzwi(liczbaDrzwi);
          samochod.setLiczbaPasazerow(liczbaPasazerow);
          samochod.setModel(modelSamochodu);
          samochod.setLiczbaPoduszek(liczbaPasazerow * 2);
          samochod.setId(1);
  
          model.addObject("msg",
                  "Details submited by you: Pojemnosc: " + silnik.getPojemnosc() + " Model: " + samochod.getModel()
                          + ", Liczba drzwi: " + samochod.getLiczbaDrzwi() + ", Liczba pasazerow: "
                          + samochod.getLiczbaPasazerow() + ", Liczba poduszek: " + samochod.getLiczbaPoduszek());
  
          SamochodDao samochodDao = new SamochodDao();
          samochodDao.dodaj(samochod);
  
          return model;
      }
  }
  </code></pre>
  
  <p>My SamochodDao class:</p>
  
  <pre><code>public class SamochodDao {
      private JdbcTemplate jdbcTemplate;
  
      @Autowired
      public void setDataSource(DataSource dataSource) {
          this.jdbcTemplate = new JdbcTemplate(dataSource);
      }
  
      @Transactional
      public void dodaj(final Samochod samochod) {
          final String engineQuery = "insert into silnik (pojemnosc) values (?)";
          KeyHolder keyHolder = new GeneratedKeyHolder();
          jdbcTemplate.update(new PreparedStatementCreator() {
              public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                  PreparedStatement ps = connection.prepareStatement(engineQuery, new String[] { "id" });
                  ps.setFloat(1, samochod.getSilnik().getPojemnosc());
                  return ps;
              }
          }, keyHolder);
  
          String carQuery = "insert into samochod (id, silnik,model,liczbaDrzwi,liczbaPasazerow,liczbaPoduszek) values (?,?,?,?,?,?)";
          Object[] inputs = new Object[] { keyHolder.getKey(), keyHolder.getKey(), samochod.getModel(),
                  samochod.getLiczbaDrzwi(), samochod.getLiczbaPasazerow(), samochod.getLiczbaPoduszek() };
          jdbcTemplate.update(carQuery, inputs);
      }
  
      public void usun(int id) {
          String query = "DELETE FROM samochod WHERE id = ?";
          Object[] inputs = new Object[] { id };
          jdbcTemplate.update(query, inputs);
          query = "DELETE FROM silnik WHERE id = ?";
          jdbcTemplate.update(query, inputs);
      }
  
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public List&lt;Samochod&gt; listuj() {
          String sql = "SELECT * FROM Silnik";
          List&lt;Silnik&gt; listaSilnikow = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Silnik.class));
          List&lt;Samochod&gt; listaSamochodow = new ArrayList();
  
          for (Silnik silnik : listaSilnikow) {
              sql = "SELECT * FROM samochod WHERE id = ?";
              Samochod samochod = (Samochod) jdbcTemplate.queryForObject(sql, new Object[] { silnik.getId() },
                      new SamochodRowMapper());
              samochod.setSilnik(silnik);
              listaSamochodow.add(samochod);
          }
          return listaSamochodow;
      }
  }
  </code></pre>
  
  <p>And my servlet.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;context:component-scan base-package="pl.edu" /&gt;
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/spring" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And when I'm trying to add new "Samochod" to database I have error:</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
  
  type Exception report
  
  message Request processing failed; nested exception is java.lang.NullPointerException
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  
  root cause
  
  java.lang.NullPointerException
      pl.edu.dao.SamochodDao.dodaj(SamochodDao.java:26)
      pl.edu.controller.StudentAdmissionController.submitAdmissionForm(StudentAdmissionController.java:48)
      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:497)
      org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
      org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
      org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
      org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
      org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p>Somebody have maybe any idea what I'm doing wrong?</p>
  
  <p>@edit</p>
  
  <p>Line 26 from SamochodDao:</p>
  
  <pre><code>jdbcTemplate.update(new PreparedStatementCreator() {
  </code></pre>
  
  <p>@edit2
  Btw. When I'm running this code as test, it works ok. Here are my beans from normal application:</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:tx="http://www.springframework.org/schema/tx"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "&gt;
  
      &lt;aop:aspectj-autoproxy /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="Silnik" class="pl.edu.lab1.Silnik"&gt;
          &lt;property name="pojemnosc" value="1.4"&gt;&lt;/property&gt;
          &lt;property name="id" value="1"&gt;&lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="Samochod" class="pl.edu.lab1.Samochod"&gt;
          &lt;property name="silnik" ref="Silnik"&gt;&lt;/property&gt;
          &lt;property name="model" value="Audi"&gt;&lt;/property&gt;
          &lt;property name="liczbaPasazerow" value="2"&gt;&lt;/property&gt;
          &lt;property name="liczbaDrzwi" value="4"&gt;&lt;/property&gt;
          &lt;property name="liczbaPoduszek" value="2"&gt;&lt;/property&gt;
          &lt;property name="id" value="1"&gt;&lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/spring" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
      &lt;bean id="SamochodDao" class="pl.edu.dao.SamochodDao"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>But when I change it to web-app I have errors.</p>