title: Spring JDBC NullPointerException
body: <p>I'm a first time poster who is a beginner at Spring. I believe I have the basic concept down, however I am running into some errors when I try to write my own code. I did my homework and I looked at other related topics on the site, but I still have not found why my code is not working.</p>
  
  <p>Please forgive me if I made any stupid mistakes. Here are all my relevant snippets of code:</p>
  
  <p>DAO Interface</p>
  
  <pre><code>public interface TestDAO {
  
  public int getId();
  
  }
  </code></pre>
  
  <p>Main SQL Connection Code  </p>
  
  <pre><code>import javax.sql.DataSource;
  
  import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  import org.springframework.jdbc.core.support.JdbcDaoSupport;
  
  
  public class TestDb extends JdbcDaoSupport implements TestDAO{
  
  private SimpleJdbcTemplate jdbcTemplate;
  
  public int getId()
  {
      String sql="SELECT DOI.DOI_ID FROM ***.DOI where DOI='test58'";
      int id=jdbcTemplate.queryForInt(sql);
      return id;
  }
  
  public void createTemplate(DataSource dataSource) {
      this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
  }
  
  }
  </code></pre>
  
  <p>beans.xml File</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"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
    &lt;bean id="webMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
      &lt;property name="mappings"&gt;
          &lt;props&gt;
              &lt;prop key="/Form.html"&gt;DoiEntry&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix"&gt;
          &lt;value&gt;/WEB-INF/jsp/&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 name="DoiEntry" class="com.****.****.DoiEntry"&gt;
      &lt;property name="eventsService" ref="eventsService"/&gt;
      &lt;property name="sendEventsQueueService" ref="sendEventsQueueService"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt; 
      &lt;property name="dataSource" ref="dataSource"/&gt; 
  &lt;/bean&gt;
  
  &lt;bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="oracle.jdbc.OracleDriver" /&gt;
      &lt;property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" /&gt;
      &lt;property name="username" value="**USER**" /&gt;
      &lt;property name="password" value="**PASSWORD**" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="testDAO" class="com.****.****.TestDAO"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="testDb" class="com.****.****.TestDb"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And finally, my web.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
  
  &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;DOILookupApp&lt;/display-name&gt;
  &lt;description&gt;
      This is a simple web application with a source code organization
      based on the recommendations of the Application Developer's Guide.
  &lt;/description&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;DOILookup&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.****.****.DoiLookup&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;DOILookup&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/DOI&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;taglib&gt;
  &lt;taglib-uri&gt;
      functions.tld
  &lt;/taglib-uri&gt;
  &lt;taglib-location&gt;
      functions.tld
  &lt;/taglib-location&gt;
  &lt;/taglib&gt;
  
      &lt;!-- The main spring MVC servlet --&gt;
  &lt;servlet&gt;
     &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
     &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
     &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;beans.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
     &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  
  &lt;/web-app&gt; 
  </code></pre>
  
  <p>Here's a JSP file I use to view it in a webpage. Disregard the srciptlet, I know they're bad coding practice, I just needed something for testing.</p>
  
  <pre><code>&lt;%@ page language="java" import="java.util.*, java.sql.*;" %&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Test DB&lt;/title&gt;
  &lt;/head&gt;
    &lt;body&gt;
  
  &lt;jsp:useBean id="testDb" scope="session" class="com.****.****.TestDb"/&gt;
  
  &lt;% 
    int output=testDb.getId();
    out.print("DOI ID for test58 is: ");
    out.print(output);
  
  
  %&gt;
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And finally, here is my error message</p>
  
  <p>type Exception report</p>
  
  <p>message</p>
  
  <pre><code>description The server encountered an internal error () that prevented it from fulfilling this request.
  
  exception
  
  org.apache.jasper.JasperException: java.lang.NullPointerException
  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  root cause
  
  java.lang.NullPointerException
  com.****.****.TestDb.getId(TestDb.java:15)
  org.apache.jsp.jsp.testDb_jsp._jspService(testDb_jsp.java:78)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  </code></pre>
  
  <p>I know this is a lot of code to look through, but thank you in advance for all your help.</p>
  
  <p>Tim</p>