0

javabean から mysql db に接続しようとすると 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
4

1 に答える 1

0

connection割り当てた直後に nullかどうかを確認します。(「データベース接続が確立されました」と出力する場所)そうであると思われ、そのDriverManager.getConnection呼び出しを本当にデバッグしたいのです。catch(Exception e){e.printStackTrace();}また、発砲していないと確信していますか?

于 2013-07-03T04:01:51.163 に答える