title: Using Singleton Class in spring mvc to get data
body: <p>I create a singleton class and calling a method inside it. I use spring mvc <code>SimpleJdbcTemplate</code> for retrieving data from database, but I get <code>NullPointerException</code>. Code I wrote is:</p>
  
  <pre><code>package in.dewsolutions.jc.utils;
  
  import in.dewsolutions.jc.dao.ReportsDAO;
  import in.dewsolutions.jc.dto.ReportsView;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.List;
  
  import javax.annotation.PostConstruct;
  import javax.sql.DataSource;
  
  import org.apache.log4j.Logger;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.config.PropertiesFactoryBean;
  import org.springframework.jdbc.core.RowMapper;
  import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
  import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Repository;
  import org.springframework.stereotype.Service;
  
  public class SingletonFilterData {
      private static final Logger logger = Logger.getLogger(SingletonFilterData.class);
  
      @Autowired
      private SimpleJdbcTemplate simpleJdbcTemplate;
  
      @Autowired
      public void setDataSource(DataSource dataSource) {
          this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
      }
  
      private SingletonFilterData() // private defaul constructor
      {
      }
  
      private static SingletonFilterData reference = null;
  
      public static SingletonFilterData getInstance() {
  
          if (reference == null) {
              synchronized (SingletonFilterData.class) {
                  if (reference == null) {
                      reference = new SingletonFilterData();
  
                  }
              }
  
          }
          return reference;
      }
  
      public List&lt;ReportsView&gt; findManuFacturer() {
          // TODO Auto-generated method stub
          List&lt;ReportsView&gt; manufacturer = new ArrayList&lt;ReportsView&gt;();
  
          ReportsView rv = new ReportsView();
          rv.setProductName("None");
          rv.setProductId(0);
          manufacturer.add(rv);
  
          String query = "SELECT * FROM manufacturer ORDER BY name";
          ParameterizedRowMapper&lt;ReportsView&gt; mapper = new ParameterizedRowMapper&lt;ReportsView&gt;() {
              public ReportsView mapRow(ResultSet rs, int rowNum)
                      throws SQLException {
                  ReportsView reportsView = new ReportsView();
                  reportsView.setProductName(rs.getString("name"));
                  reportsView.setProductId(rs.getLong("id_manufacturer"));
  
                  return reportsView;
              }
          };
          manufacturer.addAll(simpleJdbcTemplate.query(query, mapper));
          return manufacturer;
      }
  </code></pre>
  
  <p>The exception I get is:</p>
  
  <pre><code>type Exception report
  
  message
  
  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:656)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
  
  root cause
  
  java.lang.NullPointerException
      in.dewsolutions.jc.utils.SingletonFilterData.findManuFacturer(SingletonFilterData.java:77)
      in.dewsolutions.jc.controller.ReportsController.filterQuantity(ReportsController.java:169)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:597)
      org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
      org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
      org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125)
  
  note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
  </code></pre>
  
  <p>How can I remove the Exception?</p>