0

这是我第一次尝试带有 servlet(和 Java 本身)的 Java Web 应用程序。我正在尝试创建一个 MySQL 类来处理查询、连接等。无论出于何种原因。我可以获得连接,但是一旦我尝试执行查询,它就会给出一个

java.lang.NullPointerException

我很确定我犯了某种菜鸟错误。但我不清楚它是什么。

这是SQL类

package coreservlets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.mysql.jdbc.*;


public class DBInterface {
    private static Connection conn;
    private Statement statement;

    public static Connection Conn() throws SQLException, ClassNotFoundException {
        String  url = "jdbc:mysql://localhost:3306/cmwebapp";
        String user = "user";
        String pw = "password";

        try{
            //Class.forName("com.mysql.jdbc.Driver").newInstance();
            Class.forName("com.mysql.jdbc.Driver");
        conn = (Connection) DriverManager.getConnection(url, user, pw);
        //System.out.println(conn.getAutoCommit());
        } catch (SQLException e) {
            System.out.println("MySQL Connection failed.");
            e.printStackTrace();
        } 
        return conn;
    }

    public ResultSet Query(String query){
        try{
        statement = (Statement) conn.createStatement();
        ResultSet rs = statement.executeQuery(query);
        System.out.println(rs);
        return rs;
        }catch(Exception e){
            System.out.println("TestServlet.java SQL connection failed.");
            e.printStackTrace();
            return null;
        }
    }
}

我试图调用它的类

package internalLogic;
import coreservlets.*;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TheWork {
    private DBInterface conn;
    private String param;
    private ResultSet set;

    public TheWork(String p){
        try {
              conn = (DBInterface) DBInterface.Conn();
              param = "SELECT * FROM test WHERE index =" + p ;
            } catch(Exception e) {
                System.out.println("In TheWork ctor SQL connection or variable assignment failed.");
                e.printStackTrace();
            } 
    }

    public String Execute(){
        set = conn.Query(param);
        System.out.println(set);
        Object s = null;
        String ret = "";
        try{
            while (set.next()) {
                // Make container class for SQL result sets.
                if(s instanceof String){
                    s = set.getObject("index");         
                }
                if (s instanceof Integer){
                    s = String.valueOf(s);
               }
                ret += s + ", ";
            }
            return ret;
        }catch(Exception e){
            System.out.println("In TheWork Execute SQL connection or variable assignment failed.");
            e.printStackTrace();
            return null;
        }
    }
}

感谢任何想帮助我的人。

Oct 29, 2013 9:51:03 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Test] in context with path [/test-app] threw exception
java.lang.NullPointerException
    at internalLogic.TheWork.Execute(TheWork.java:26)
    at testPackage.TestServlet.doGet(TestServlet.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

0

在您的Execute()方法中,实例conn可能是null. 我认为在你的TheWork课上:

private DBInterface conn;

应该

private Connection conn;

并在构造函数中

conn = (DBInterface) DBInterface.Conn();

应该很简单

conn = DBInterface.Conn();

更新:(上面建议的更正没有用)

在您的最后评论之后,我已经意识到发生了什么;真正的问题是您需要DBInterface从您的Conn()方法返回一个实例。如果您使用原始代码并Conn()像我在下面所做的那样更改方法,它应该可以解决问题...

public static DBInterface Conn() throws SQLException, ClassNotFoundException {
    String  url = "jdbc:mysql://localhost:3306/cmwebapp";
    String user = "user";
    String pw = "password";
    DBInterface dbInterface = new DBInterface();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        dbInterface.conn = (Connection) DriverManager.getConnection(url, user, pw);
    } catch (SQLException e) {
        System.out.println("MySQL Connection failed.");
        e.printStackTrace();
    }
    return dbInterface;
}
于 2013-10-29T17:21:48.110 回答