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)