我的项目有问题
我的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());
}
}
我有2个班级
public class Category {
private long categoryID;
private String categoryName;
第二类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());
}
}
} 和我的JSP
<%
CategoryDAO categoryDAO = new CategoryDAO();
for (Category c : categoryDAO.getListCategory()) {%>
<li><a href="products.html"><%=c.getCategoryName()%></a></li>
<%}%>
当我运行getListCategory的类时,数据显示相关。 但是当我尝试在JSP中显示它时,NullPointException会出现。 当我使用Neatbean时,这个JSP工作正常。但是使用Eclipse,会出现此错误。 抱歉我的英语,但谁可以帮我解决这个错误。 我是新手:(
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)