Chào mọi người! Em hiện tại đang bắt đầu học JSP Servlet theo một vài video hướng dẫn trên mạng và gặp phải lỗi này ạ…
public class Category {
private long categoryID;
private String categoryName;
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;
}
Trang JSP của em
<%
CategoryDAO categoryDAO = new CategoryDAO();
for (Category c : categoryDAO.getListCategory()) {%>
<li><a href="products.html"><%=c.getCategoryName()%></a></li>
<%}%>
Em bị bị báo Null ở đoạn lệnh for (Category c : categoryDAO.getListCategory())
mà không biết tại sao.
Bên class CategoryDAO em chạy thử thì kết nối thành công và hiển thị được dữ liệu trong DB. Nhưng khi đưa vào trang JSP thì lại bị báo là NullPointerException.
Video em xem là của KenhLapTrinh, có rất nhiều bạn cũng gặp tình trạng tương tự khi sử dụng Eclipse, với Netbean thì lại không gặp phải lỗi này ạ.
Các package em đều import đầy đủ, mong được mọi người giải đáp ạ