NullPointerException可能与DataSource连接有关

时间:2014-09-07 12:50:30

标签: java mysql tomcat servlets datasource

我正在尝试在Web应用程序中首次使用连接池。目前,当我尝试加载显示从MySQL数据库中检索到2个推荐的主页时,我得到了这个堆栈跟踪:

  

显示java.lang.NullPointerException     uk.co.morleys.TestimonialService.getTestimonials(TestimonialService.java:57)     uk.co.morleys.HomeController.doGet(HomeController.java:54)     javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     javax.servlet.http.HttpServlet.service(HttpServlet.java:728)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)     org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)     org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)     org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)     org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)     org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)     org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)     org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)     org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)     org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)

此错误与以下方法有关:

public class TestimonialService {
    ResultSet rs;

    public TestimonialService(){
        rs = null;
    }
    public List<Testimonial> getTestimonials(int limit) throws SQLException{
        List<Testimonial> list = new ArrayList<Testimonial>();
        DataSource dd = DataSourceFactory.getMySQLDataSource();

        try(Connection con = dd.getConnection()){
            PreparedStatement ps = con.prepareStatement("SELECT id, author, testimonial, date FROM morleys_testimonial WHERE isActive=1 ORDER BY date DESC LIMIT ?");
            ps.setInt(1, limit);
            rs = ps.executeQuery();
            while(rs.next()){
                Testimonial Testimonial = new Testimonial();
                Testimonial.setAuthor(rs.getString("author"));
                Testimonial.setTestimonial(rs.getString("testimonial"));
                Testimonial.setDate(rs.getString("date"));
                Testimonial.setID(rs.getInt("id"));
                list.add(Testimonial);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return list;
    }
}

这是DataDourceFactory类:

public class DataSourceFactory {

    public static DataSource getMySQLDataSource() {
        Properties props = new Properties();
        FileInputStream fis = null;
        MysqlDataSource mysqlDS = null;
        try {
            fis = new FileInputStream("db.properties");
            props.load(fis);
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
            mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
            mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mysqlDS;
    }

}

这就是我将主页提供给浏览器的方式:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String page = getPageName(request.getParameter("page"));
    switch (page){
            case "home":
                List<Testimonial> testimonialList;
                try {
                    testimonialList = testimonialService.getTestimonials(2);
                    request.setAttribute("testimonialList", testimonialList);
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }

                break;
RequestDispatcher rd = getServletContext().getRequestDispatcher(views + getPageFilename(page));
        rd.forward(request, response);
    }

问题 导致NullPointerException的原因是什么。我假设它与DataSource有关。我是否正确建立了与数据库的连接?

UPDATE 添加了`db.properties文件

#mysql DB properties
MYSQL_DB_DRIVER_CLASS=com.mysql.jdbc.Driver
MYSQL_DB_URL=jdbc:mysql://myhostname/
MYSQL_DB_USERNAME=username
MYSQL_DB_PASSWORD=password

File structure

1 个答案:

答案 0 :(得分:2)

fis = new FileInputStream("db.properties");

此行投掷FileNotFoundException。您正在捕获异常并在return mysqlDS;

中返回空值

因此DataSource dd为null,当您访问它时,会抛出空指针异常。

您需要确保db.properties可以从您的Java程序中读取。要么是完整路径,要么确保它是jar的一部分,可以通过类加载器找到它。