java web java.lang.NullPointerException

严重: Servlet.service() for servlet [selectServlet] in context with path [/Exercise] threw exception
java.lang.NullPointerException
at DB.DataBaseConnection.selectOperation1(DataBaseConnection.java:45)
at Servlet.Select.doPost(Select.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:619)

代码:
public class DataBaseConnection 

{
static String driverString="com.mysql.jdbc.Driver";
static String urlString="jdbc:mysql://localhost:3306/book";
static String username="root";
static String password="123";

public static Connection getConnection()
{
    Connection connection;
    try {
        Class.forName(driverString);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
        // TODO: handle exception
    }
    try {
        connection=DriverManager.getConnection(urlString, username, password);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
        // TODO: handle exception
    }
    return connection;
}

public static ResultSet selectOperation1(String Content,String SelectWay)
{
    Connection connection=getConnection();
    Statement statement=null;
    try {
        statement=connection.createStatement();//这一行报错。。
        String sqlString="select * from bookinfo where 1=1";
        if(Content!=""&&SelectWay!="")
        {
            sqlString+="and"+SelectWay+"='"+Content+"'";
        }

        ResultSet rs=statement.executeQuery(sqlString);
        return rs;
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
        // TODO: handle exception
    }

}

2个回答

我碰到过这个问题,我用的是javaEE,如果你这个项目是web项目,需要把mysql-connector-java-5.1.7-bin,jarj包,手动复制到tomcat的lib目录下,因为Class.forName(driverString);时,javaEE不会去查找驱动的,所以会出现空指针异常,而如果是java项目就不用了。

数据驱动jar包没有,检查一下,web,xml的Sevrlet的配置

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问