所以,我有一个 jsp 文件 (ShowProducCatalog.jsp),它试图通过 javabean (ProductDataBean.java) 从 mysql 数据库中检索数据。我正在使用 netbeans IDE。我通过从 netbeans ids 创建表并在 mysql 工作台中查看同一个表来确保服务器正在运行。但是每次我启动我的应用程序时,我都会收到此错误
org.apache.jasper.JasperException: An exception occurred processing JSP page /ShowProductCatalog.jsp at line 9
6:
7: <html>
8: <body>
9: <% List productList = data.getProductList();
10: Iterator prodListIterator = productList.iterator();
11: %>
12:
根本原因来自我的 bean 根本原因
java.lang.NullPointerException
cart.ProductDataBean.getProductList(ProductDataBean.java:36)
org.apache.jsp.ShowProductCatalog_jsp._jspService(ShowProductCatalog_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
这是我的 ProductDataBean 部分,错误来自:
public class ProductDataBean implements Serializable
{
private static Connection connection;
public ProductDataBean()
{
try
{
String userName = "root";
String password = "root";
String url = "jdbc:mysql://localhost:/eshopdb";
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(url,userName,password);
System.out.println("Database connection established");
}catch(Exception e){e.printStackTrace();}
}
public static Connection getConnection()
{
return connection;
}
public ArrayList getProductList() throws SQLException
{
ArrayList productList = new ArrayList();
Statement statement = connection.createStatement();//ERROR HERE
ResultSet results = statement.executeQuery("SELECT * FROM product");
while (results.next())
{...
似乎连接变量从未初始化过!但我的用户名、密码和网址是正确的。请帮忙!