0

I have a problem with my project

My DBConnect

package shop.connect;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {
    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456789");
            System.out.println("Connect succesful");
        } catch (Exception e) {
            // TODO: handle exception
        }
        return connection;
    }

    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}

I have 2 class

public class Category {
    private long categoryID;
    private String categoryName;

And second class CategoryDAO

public class CategoryDAO {

// get danh sach the loai
public ArrayList<Category> getListCategory() throws SQLException {
    Connection connection = DBConnect.getConnection();
    String sql = "SELECT * from category";
    PreparedStatement pStatement = connection.prepareCall(sql);
    ResultSet rSet = pStatement.executeQuery();
    ArrayList<Category> list = new ArrayList<>();
    while (rSet.next()) {
        Category category = new Category();
        category.setCategoryID(rSet.getInt("category_id"));
        category.setCategoryName(rSet.getString("category_name"));
        list.add(category);
    }
    return list;
}

public static void main(String[] args) throws SQLException {
    CategoryDAO dao = new CategoryDAO();
    for (Category ds : dao.getListCategory()) {
        System.out.println(ds.getCategoryID() + " - " + ds.getCategoryName());
    }
}

} And my JSP

<%
CategoryDAO categoryDAO = new CategoryDAO();
for (Category c : categoryDAO.getListCategory()) {%>
<li><a href="products.html"><%=c.getCategoryName()%></a></li>
<%}%>

When i run getListCategory's class, data display corret. But when i try to display it in JSP, NullPointException appers. When i use Neatbean, this JSP work fine. But with Eclipse, this bug appears. Sorry about my English but who can help me fix this bug. I'm newbie :(

Type Exception Report

Message An exception occurred processing JSP page /header.jsp at line 97

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /header.jsp at line 97

94:                         <li><a href="#">Laptops & Notebooks</a>
95:                             <ul class="drop">
96:                                 <%
97:                                     for( Category c: categoryDAO.getListCategory()){
98:                                 %>
99:                                 <li><a href="products.html"><%=c.getCategoryName()%></a></li>
100:                                <%


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.lang.NullPointerException
    shop.dao.CategoryDAO.getListCategory(CategoryDAO.java:18)
    org.apache.jsp.header_jsp._jspService(header_jsp.java:215)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  • Post the stacktrace. – Jozef Chocholacek Apr 12 '17 at 12:34
  • I added stracktrace on my post. Please help me :( – Khuất Duy Tân Apr 12 '17 at 16:30
  • What line is `CategoryDAO.java:18`? I suspect it is the `PreparedStatement pStatement = connection.prepareCall(sql);` line, which would mean that your `DBConnect.getConnection()` does not return a connection when running in JSP context. To be able to help you, you have to post the source of the `DBConnect` class. – Jozef Chocholacek Apr 12 '17 at 17:53
  • Thank you, i added DBConnect class and all source in CategoryDAO class. Please help me – Khuất Duy Tân Apr 13 '17 at 02:48
  • `// TODO: handle exception` in `DBConnect` deserves a slap... put at least `e.printStackTrace()` there! I am 99.99% sure you are missing the DB driver in the proper place - it has to be in `WEB-INF/lib` folder of your webapp - the silently ignored exception would have shown it. – Jozef Chocholacek Apr 13 '17 at 06:10

0 Answers0