我创建一个单例类并在其中调用一个方法。我使用spring mvc SimpleJdbcTemplate
从数据库中检索数据,但我得到了NullPointerException
。我写的代码是:
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<ReportsView> findManuFacturer() {
// TODO Auto-generated method stub
List<ReportsView> manufacturer = new ArrayList<ReportsView>();
ReportsView rv = new ReportsView();
rv.setProductName("None");
rv.setProductId(0);
manufacturer.add(rv);
String query = "SELECT * FROM manufacturer ORDER BY name";
ParameterizedRowMapper<ReportsView> mapper = new ParameterizedRowMapper<ReportsView>() {
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;
}
我得到的例外是:
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.
如何删除例外?
答案 0 :(得分:1)
使用new SingletonFilterData()
不会调用自动装配,因此simpleJdbcTemplate
仍为null
。
您可以配置Spring,而不是实现getInstance()
,以便SingletonFilterData
是单身并注入ReportsController
。