当我尝试从javabean连接到mysql数据库时出现NullPointerException

时间:2013-07-03 03:55:34

标签: jdbc javabeans

当我尝试从javabean

连接到mysql数据库时出现NullPointerException
org.apache.jasper.JasperException: An exception occurred processing JSP page     /ShowProductCatalog.jsp at line 9
<jsp:useBean id= "data" class= "cart.ProductDataBean" scope="request"/>

<html>
    <body>
8:     <body>
9:         <%  List productList = data.getProductList();
10:             Iterator prodListIterator = productList.iterator();
11:           %> 

根本原因

java.lang.NullPointerException
    cart.ProductDataBean.getProductList(ProductDataBean.java:36)

ProductDataBean.java

    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();
/**********************HERE IS LINE 36. ERROR HERE*******************************/
            Statement statement = connection.createStatement();//ERROR HERE
            ResultSet results = statement.executeQuery("SELECT * FROM product");

        Help

1 个答案:

答案 0 :(得分:0)

分配后,查看connection是否为空。 (你打印“建立数据库连接”)我怀疑它是,你真的想调试DriverManager.getConnection调用。另外,你确定catch(Exception e){e.printStackTrace();}没有被解雇吗?