xiaxiaguagua
xiaxiaguagua
2015-05-10 10:37

java web java.lang.NullPointerException

  • ava web

严重: 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条回答