crmepham
crmepham

Reputation: 4760

NullPointerException possibly related to DataSource connection

I am attempting to use connection pooling for the first time in a web application. Currently I get this stacktrace when I attempt to load the home webpage which displays 2 testimonials retrieved from a MySQL database:

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)

This error relates to the following method:

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;
    }
}

Here is the DataDourceFactory class:

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;
    }

}

and this is how I serve the home page to the browser:

    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);
    }

Question What is causing the NullPointerException. I am assuming it is something to do with the DataSource. Have I established a connection to the database correctly?

UPDATE Added `db.properties file

#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

Upvotes: 0

Views: 965

Answers (1)

Ravi Kiran
Ravi Kiran

Reputation: 1139

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

This line is throwing a FileNotFoundException. You are catching the exception and returning a null value in return mysqlDS;

So the DataSource dd is null and when you access it, a null pointer exception is thrown.

You need to ensure that the db.properties is readable from your java program. Either with a full path or by ensuring that it is part of a jar where it can be located by the class loader.

Upvotes: 2

Related Questions